beforeEach(()=>{
      event = TH.Factory.createEvent();
      user = TH.Factory.createUser();
      TH.noInfo();

      stub(Val, 'ensureString');
    });
    test("authorize", ()=>{
      var org = TH.Factory.createOrg();
      var user = TH.Factory.createUser('su');

      refute.accessDenied(()=>{
        org.authorize(user._id);
      });

      user = TH.Factory.createUser();

      TH.noInfo();

      assert.accessDenied(()=>{
        org.authorize(user._id);
      });
    });
      test("denied", ()=>{
        const oOrg = TH.Factory.createOrg();
        const oEvent = TH.Factory.createEvent();
        const oUser = TH.Factory.createUser();

        const competitor = TH.Factory.buildCompetitor();

        assert.accessDenied(()=>{competitor.authorize(user._id)});
      });
      test("wrong org denied", ()=>{
        const oOrg = TH.Factory.createOrg();
        const oUser = TH.Factory.createUser();

        const event = TH.Factory.buildEvent();

        assert.accessDenied(()=>{
          event.authorize(user._id);
        });
      });
      test("denied", ()=>{
        const oOrg = TH.Factory.createOrg();
        const oUser = TH.Factory.createUser();

        const result = TH.Factory.buildResult();

        assert.accessDenied(()=>{
          result.authorize(user._id);
        });
      });
      test("denied", ()=>{
        const oOrg = TH.Factory.createOrg();
        const oUser = TH.Factory.createUser();

        const teamType = TH.Factory.buildTeamType();

        assert.accessDenied(()=>{
          teamType.authorize(user._id);
        });
      });
      test("wrong org denied", ()=>{
        const oOrg = TH.Factory.createOrg();
        const oUser = TH.Factory.createUser();

        const series = TH.Factory.buildSeries();

        TH.noInfo();
        assert.accessDenied(()=>{
          series.authorize(user._id);
        });
      });
    beforeEach(()=>{
      v.org =TH.Factory.createOrg();
      v.user = TH.Factory.createUser({role: User.ROLE.admin});
      v.event = TH.Factory.createEvent({heats: undefined});
      v.clubType = TH.Factory.createTeamType({name: 'Club'});
      v.team = TH.Factory.createTeam({name: 'Rock Hoppers'});
      v.fjl = TH.Factory.createCategory({
        shortName: 'FJL', gender: 'f', name: 'Female Junior Lead', group: 'Youth Lead'});
      v.mjl = TH.Factory.createCategory({
        shortName: 'MJL', gender: 'm', name: 'Male Junior Lead', group: 'Youth Lead'});
      v.fol = TH.Factory.createCategory({
        shortName: 'FOL', gender: 'f', name: 'Female Open Lead', group: 'Open Lead'});
      v.sam = TH.Factory.createClimber({
        name: 'Sam Smith', dateOfBirth: '1996-04-16', number: 333});
      TH.loginAs(v.user);

      v.rpc = TH.mockRpc();
    });
 beforeEach(()=>{
   org = TH.Factory.createOrg();
   user = TH.Factory.createUser();
   TH.noInfo();
 });
define((require, exports, module)=>{
  const session         = require('koru/session');
  const TH              = require('test-helper');

  const {stub, spy, onEnd} = TH;

  const User = require('./user');

  TH.testCase(module, {
    tearDown() {
      TH.clearDB();
    },

    "test isSuperUser"() {
      var user = TH.Factory.buildUser('su');

      assert.isFalse(user.isSuperUser());
      user.attributes.role = 's';

      assert.isTrue(user.isSuperUser());

      user.attributes.role = 'x';
      assert.isFalse(user.isSuperUser());
    },

    "test isAdmin"() {
      var adminUser = TH.Factory.createUser({role: 'a'});
      var user = TH.Factory.createUser({role: 'j'});
      var su = TH.Factory.createUser('su');

      assert.isFalse(user.isAdmin());
      user.role = 'a';
      assert.isFalse(user.isAdmin());

      assert.isTrue(adminUser.isAdmin());
      assert.isTrue(su.isAdmin());

    },

    "test canAdminister, canJudge"() {
      var doc = new User({org_id: '123'});
      doc.attributes.role = User.ROLE.superUser;

      refute(doc.canAdminister());
      refute(doc.canAdminister('x'));

      doc.attributes.role = User.ROLE.admin;

      const event = {attributes: {org_id: doc.org_id}};

      refute(doc.canAdminister());
      assert(doc.canAdminister(event));
      assert(doc.canAdminister({attributes: {}, org_id: doc.org_id}));
      refute(doc.canAdminister({attributes: {org_id: '456'}, org_id: doc.org_id}));
      assert(doc.canJudge(event));

      doc.attributes.role = User.ROLE.judge;
      doc.changes.role = User.ROLE.superUser;

      assert.same(doc.safeRole, User.ROLE.judge);

      refute(doc.canAdminister(event));
      assert(doc.canJudge(event));

      doc.attributes.role = User.ROLE.climber;

      refute(doc.canJudge(event));
    },

    "test forgotPassword"() {
      stub(session, 'rpc');
      const cb = stub();

      User.forgotPassword('email@address', cb);

      assert.calledWith(session.rpc, 'User.forgotPassword', 'email@address', cb);
    },
  });
});
 beforeEach(()=>{
   rpc = TH.mockRpc();
   org = TH.Factory.createOrg();
   user = TH.Factory.createUser();
 });
 beforeEach(()=>{
   org = TH.Factory.createOrg();
   user = TH.Factory.createUser();
   stub(koru, 'info');
 });