<commit_before>var setup = require(require('root-path')('test/setup'));

describe('UserController', function() {

  var noop = () => () => this,
    req, res;

  beforeEach(function() {
    req = {
      allParams: () => this.params,
      param: key => this.params[key],
      __: sails.__
    };
    return setup.clearDb();
  });

  describe('.create', function() {
    var community;

    beforeEach(function() {
      res = {
        send: console.error,
        status: spy(noop()),
        ok: spy(noop())
      };

      UserSession.login = spy(function() {});
      User.create = spy(User.create);

      community = new Community({beta_access_code: 'foo', name: 'foo', slug: 'foo'});
      return community.save();
    });

    it('works with a username and password', function() {
      _.extend(req, {
        params: {
          email: '<EMAIL>',
          password: 'password!',
          code: 'foo',
          login: true
        },
        session: {}
      });

      return UserController.create(req, res).then(function() {
        expect(res.status).not.to.have.been.called();
        expect(User.create).to.have.been.called();
        expect(UserSession.login).to.have.been.called();
        expect(res.ok).to.have.been.called();

        return User.where({email: '<EMAIL>'}).fetch({withRelated: ['onboarding']});
      })
      .then(function(user) {
        var onboarding = user.relations.onboarding;
        expect(onboarding).not.to.be.null;
        expect(onboarding.get('user_id')).to.equal(user.id);
        expect(onboarding.get('type')).to.equal('onboarding');
        expect(onboarding.get('status').step).to.equal('start');
      });
    });

    describe('with an invitation to a community', function() {

      var invitation;

      beforeEach(() => {
        var inviter = new User({email: '<EMAIL>'});
        return inviter.save()
        .then(() => Invitation.create({
          communityId: community.id,
          userId: inviter.id,
          email: "<EMAIL>"
        }))
        .tap(i => invitation = i);
      });

      it('works', function() {
        _.extend(req, {
          params: {
            email: '<EMAIL>',
            password: 'password!',
            login: true
          },
          session: {invitationId: invitation.id}
        });

        return UserController.create(req, res).then(function() {
          expect(res.status).not.to.have.been.called();
          expect(User.create).to.have.been.called();
          expect(UserSession.login).to.have.been.called();
          expect(res.ok).to.have.been.called();

          return User.where({email: '<EMAIL>'}).fetch({withRelated: ['communities']});
        })
        .then(user => {
          var community = user.relations.communities.first();
          expect(community).to.exist;
          expect(community.get('name')).to.equal('foo');
        });
      });

    });

  });

  describe('with an existing user', function() {
    var u1, u2;

    beforeEach(function() {
      u1 = new User({email: '<EMAIL>'});
      u2 = new User({email: '<EMAIL>'});
      return Promise.join(u1.save(), u2.save());
    });

    describe('.update', function() {

      it('halts on invalid email', function(done) {

        _.extend(req, {
          params: {userId: u1.id, email: 'lol'}
        });

        res = {
          badRequest: function(message) {
            expect(message).to.equal(sails.__('invalid-email'));
            done();
          },
          serverError: done
        };

        UserController.update(req, res);
      });

      it('halts on duplicate email', function(done) {
        _.extend(req, {
          params: {userId: u1.id, email: u2.get('email')}
        });

        res = {
          badRequest: function(message) {<commit_msg>fix tests<commit_after>var setup = require(require('root-path')('test/setup'));

describe('UserController', function() {

  var noop = () => () => this,
    req, res;

  beforeEach(function() {
    req = {
      allParams: () => this.params,
      param: key => this.params[key],
      __: sails.__
    };
    return setup.clearDb();
  });

  describe('.create', function() {
    var community;

    beforeEach(function() {
      res = {
        send: console.error,
        status: spy(noop()),
        ok: spy(noop())
      };

      UserSession.login = spy(function() {});
      User.create = spy(User.create);

      community = new Community({beta_access_code: 'foo', name: 'foo', slug: 'foo'});
      return community.save();
    });

    it('works with a username and password', function() {
      _.extend(req, {
        params: {
          email: '<EMAIL>',
          password: 'password!',
          code: 'foo',
          login: true
        },
        session: {}
      });

      return UserController.create(req, res).then(function() {
        expect(res.status).not.to.have.been.called();
        expect(User.create).to.have.been.called();
        expect(UserSession.login).to.have.been.called();
        expect(res.ok).to.have.been.called();

        return User.where({email: '<EMAIL>'}).fetch({withRelated: ['onboarding']});
      })
      .then(function(user) {
        var onboarding = user.relations.onboarding;
        expect(onboarding).not.to.be.null;
        expect(onboarding.get('user_id')).to.equal(user.id);
        expect(onboarding.get('type')).to.equal('onboarding');
        expect(onboarding.get('status').step).to.equal('start');
      });
    });

    describe('with an invitation to a community', function() {

      var invitation;

      beforeEach(() => {
        var inviter = new User({email: '<EMAIL>'});
        return inviter.save()
        .then(() => Invitation.create({
          communityId: community.id,
          userId: inviter.id,
          email: "<EMAIL>"
        }))
        .tap(i => invitation = i);
      });

      it('works', function() {
        _.extend(req, {
          params: {
            email: '<EMAIL>',
            password: 'password!',
            login: true
          },
          session: {invitationId: invitation.id}
        });

        return UserController.create(req, res).then(function() {
          expect(res.status).not.to.have.been.called();
          expect(User.create).to.have.been.called();
          expect(UserSession.login).to.have.been.called();
          expect(res.ok).to.have.been.called();

          return User.where({email: '<EMAIL>'}).fetch({withRelated: ['communities']});
        })
        .then(user => {
          var community = user.relations.communities.first();
          expect(community).to.exist;
          expect(community.get('name')).to.equal('foo');
        });
      });

    });

  });

  describe('with an existing user', function() {
    var u1, u2;

    beforeEach(function() {
      u1 = new User({email: '<EMAIL>', active: true});
      u2 = new User({email: '<EMAIL>', active: true});
      return Promise.join(u1.save(), u2.save());
    });

    describe('.update', function() {

      it('halts on invalid email', function(done) {

        _.extend(req, {
          params: {userId: u1.id, email: 'lol'}
        });

        res = {
          badRequest: function(message) {
            expect(message).to.equal(sails.__('invalid-email'));
            done();
          },
          serverError: done
        };

        UserController.update(req, res);
      });

      it('halts on duplicate email', function(done) {
        _.extend(req, {
          params: {userId: u1.id, email: u2.get('email')}
        });

        res = {
          badRequest: function(message) {
{'fraction_non_alphanumeric': 0.17735550277117973, 'fraction_numerical': 0.0023752969121140144, 'mean_word_length': 2.085912052117264, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '1450455', 'n_tokens_mistral': 2314, 'n_tokens_neox': 2225, 'n_words': 399}