{"size": 2173, "ext": "js", "max_stars_repo_path": "test/integration/controllers/ActivityController.test.js", "max_stars_repo_name": "EsmeMedina/openopps-platform", "max_stars_repo_stars_event_min_datetime": "2015-11-25T21:35:08.000Z", "max_stars_repo_stars_event_max_datetime": "2017-03-25T22:56:23.000Z", "max_issues_repo_path": "test/integration/controllers/ActivityController.test.js", "max_issues_repo_name": "EsmeMedina/openopps-platform", "max_issues_repo_issues_event_min_datetime": "2015-11-18T15:01:43.000Z", "max_issues_repo_issues_event_max_datetime": "2016-11-21T18:22:26.000Z", "max_forks_repo_path": "test/integration/controllers/ActivityController.test.js", "max_forks_repo_name": "EsmeMedina/openopps-platform", "max_forks_repo_forks_event_min_datetime": "2015-11-28T08:58:28.000Z", "max_forks_repo_forks_event_max_datetime": "2021-02-14T11:42:28.000Z"}
{"max_stars_count": 34, "max_issues_count": 176, "max_forks_count": 25, "avg_line_length": 30.6056338028, "max_line_length": 100, "alphanum_fraction": 0.5701794754}
var _ = require('lodash');
var assert = require('chai').assert;
var request = require('supertest');
var resAssert = require('./resAssert');

describe('ActivityController', function() {
  var user;
  var agent;

  beforeEach(function(done) {
    user = require('../../fixtures/user').minAttrs;
    var newUserAttrs = user;

    User.register(user, function(err, createdUser) {
      user = createdUser;
      assert.isNull(err);
      agent = request.agent(sails.hooks.http.app);
      agent.post('/api/auth/local')
        .send({
          identifier: newUserAttrs.username,
          password: newUserAttrs.password,
          json: true
        })
        .expect(200)
        .end(done)
    });
  });

  describe('api/activity/badges', function() {
    it('should return an empty list when the user has no badges', function(done) {
      agent.get('/api/activity/badges')
        .expect(200)
        .expect(function(res) {
          assert.deepEqual(res.body, []);
        })
        .end(done)
    });

    it('should return a list containing recent badges for a task', function(done) {
      var task, emmy;
      var emmyAttrs = require('../../fixtures/user').emmyAttrs;

      Task.create({'userId': user.id}).then(function(newTask) {
        task = newTask;
        return User.create(emmyAttrs)
      })
      .then(function(newUser) {
        emmy = newUser
        return Volunteer.create({userId:emmy.id, taskId:task.id})
      })
      .then(function(newVolunteer) {
        Task.update(task.id, {id: task.id, state: 'completed'}).exec(function (err, updated_task) {
          user.completedTasks = 1;

          Badge.awardForTaskCompletion(task, user, function(err, badges) {
            assert.isNull(err);
            agent.get('/api/activity/badges')
              .expect(200)
              .expect(function(res) {
                assert.equal(res.body.length, 1);
                assert.equal(res.body[0].badges.length, 1, "there should be one badge");
                assert.equal(res.body[0].participants.length, 1, "there should be one participant");
              })
              .end(done)
          });
        });
      });
    });
  });
});
