<commit_before><commit_msg>add tests for channels route<commit_after>#!/usr/bin/env node

var vows = require('vows'),
    assert = require('assert'),
    request = require('request'),
    baseurl = 'http://127.0.0.1:32773',
    j1 = request.jar(),
    j2 = request.jar(),
    userdata1 = {},
    userdata2 = {},
    cid1 = 'cid_1',
    cid2 = 'cid_2';

vows.describe('Channels route').addBatch({

  'when a client join a channel': {
    topic: function () {
      var self = this;

      // auth
      request({
        method: 'GET',
        url: baseurl+'/server/auth',
        headers: { 'Pfc-Authorization': 'Basic '+new Buffer("testch:testchpassword").toString('base64') }, 
        jar: j1,
      }, function (err, res, body) {
        userdata1 = JSON.parse(body);

        // join the channel cid1
        request({
          method: 'POST',
          url: baseurl+'/server/channels/'+cid1+'/users/',
          jar: j1,
        }, self.callback);

      });
    },
    'server returns the user list': function (error, res, body) {
      assert.equal(res.statusCode, 200);

      try {
        var userlist = JSON.parse(body);
      } catch(err) {
        assert.isNull(err, 'response body should be JSON formated');
      }

      assert.lengthOf(Object.keys(userlist), 1);
      assert.include(Object.keys(userlist), userdata1.id);
    },

    'and another user joins': {
      topic: function () {
        var self = this;

        // auth user2
        request({
          method: 'GET',
          url: baseurl+'/server/auth',
          headers: { 'Pfc-Authorization': 'Basic '+new Buffer("testch2:testch2password").toString('base64') }, 
          jar: j2,
        }, function (err, res, body) {
          userdata2 = JSON.parse(body);

          // join the channel cid1
          request({
            method: 'POST',
            url: baseurl+'/server/channels/'+cid1+'/users/',
            jar: j2,
          }, self.callback);

        });
      },
      'server also returns the user list': function (error, res, body) {
        assert.equal(res.statusCode, 200);

        try {
          var userlist = JSON.parse(body);
        } catch(err) {
          assert.isNull(err, 'response body should be JSON formated');
        }

        assert.lengthOf(Object.keys(userlist), 2);
        assert.include(Object.keys(userlist), userdata1.id);
        assert.include(Object.keys(userlist), userdata2.id);
      },
      'the user list contains full userdata': function (error, res, body) {
        try {
          var userlist = JSON.parse(body);
        } catch(err) {
          assert.isNull(err, 'response body should be JSON formated');
        }
        assert.equal(userlist[userdata1.id].name, userdata1.name);
        assert.equal(userlist[userdata2.id].name, userdata2.name);
      },
    },
  },

}).addBatch({

  'when a joined user want to list users\'s channel': {
    topic: function () {
      var self = this;

      // list users in channel1
      request({
        method: 'GET',
        url: baseurl+'/server/channels/'+cid1+'/users/',
        jar: j1,
      }, self.callback);

    },
    'server returns ok': function (error, res, body) {
      assert.equal(res.statusCode, 200);
    },
    'server returns user ids of this channel': function (error, res, body) {
      try {
        var userids = JSON.parse(body);
      } catch(err) {
        assert.isNull(err, 'response body should be JSON formated');
      }
      assert.lengthOf(userids, 2);
      assert.include(userids, userdata1.id);
      assert.include(userids, userdata2.id);
    },
  },

}).export(module);
{'fraction_non_alphanumeric': 0.15043019705800723, 'fraction_numerical': 0.01665278934221482, 'mean_word_length': 2.12034632034632, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2621980', 'n_tokens_mistral': 1143, 'n_tokens_neox': 1108, 'n_words': 259}