def setUp(self): ''' set up test fixtures ''' self.ledger = OcLedger() # set up regular user self.userReg = OcUser().setup_user( username='******', email='*****@*****.**', ) # set up user with a npf affiliation self.userOrg = OcUser().setup_user( username='******', email='*****@*****.**', ) # set up user with a biz affiliation self.userBiz = OcUser().setup_user( username='******', email='*****@*****.**', ) # set up test org self.orgTest = OcOrg().setup_org(name='test_org_123', status='npf') self.projectUserRegMT = Project(org=self.orgTest, name='MT by userReg') self.projectUserRegMT.save() self.eventUserRegMT = Event( project=self.projectUserRegMT, description='MT by userReg', location='test', coordinator=self.userOrg, creator_id=self.userOrg.id, event_type='MN', datetime_start=datetime.now(tz=pytz.utc) - timedelta(hours=2), datetime_end=datetime.now(tz=pytz.utc) - timedelta(hours=1)) self.eventUserRegMT.save() self.userRegTimeLog = UserTimeLog( user=self.userReg, event=self.eventUserRegMT, is_verified=True, datetime_start=datetime.now(tz=pytz.utc), datetime_end=datetime.now(tz=pytz.utc) + timedelta(hours=1)) self.userRegTimeLog.save() self.actionUserReg = AdminActionUserTime( user=self.userOrg, usertimelog=self.userRegTimeLog, action_type='app') self.actionUserReg.save() # issue currents to userReg self.ledger.issue_currents(self.orgTest.orgentity.id, self.userReg.userentity.id, self.actionUserReg, 1)
def test_view_displays_up_to_10_active_biz_last_month(self): # Create 5 bizs # Each of them has admin, project, event, user with timelog accepted by admin and issued transaction [self.set_up_org(status='biz') for _ in range(5)] # Random transaction amounts are recorded to the list # And compared to interface output top_biz_last_month = OcOrg().get_top_bizs('month') self.assertEqual(top_biz_last_month[0], self.top_with_names[0]) # Create 10 more organisations to check that interface method outputs 10 items [self.set_up_org(status='biz') for _ in range(10)] top_biz_last_month = OcOrg().get_top_bizs('month') self.assertEqual(len(top_biz_last_month), 10)
def test_view_displays_up_to_10_active_npf_all_time(self): # Create 5 npfs # Each of them has admin, project, event, user with timelog accepted by admin and issued transaction [self.set_up_org(status='npf', old=True) for _ in range(5)] # There were no transactions for last month top_npf_last_month = OcOrg().get_top_issued_npfs('month') self.assertEqual(top_npf_last_month[0]['total'], 0) # Random transaction amounts are recorded to the list # And compared to interface output top_npf_all_time = OcOrg().get_top_issued_npfs('all-time') self.assertEqual(top_npf_all_time[0], self.old_top_with_names[0]) # Create 10 more organisations to check that interface method outputs 10 items [self.set_up_org(status='npf', old=True) for _ in range(10)] top_npf_all_time = OcOrg().get_top_issued_npfs('all-time') self.assertEqual(len(top_npf_all_time), 10)
def setUp(self): """Set up test fixtures.""" _TEST_UUID = uuid.uuid4() self.client = Client() # set up regular user userRegEmail = '*****@*****.**' % _TEST_UUID self.userReg = OcUser().setup_user( username=userRegEmail, email=userRegEmail, ) # set up user with a npf affiliation userOrgEmail = '*****@*****.**' % _TEST_UUID self.userOrg = OcUser().setup_user( username=userOrgEmail, email=userOrgEmail, ) # set up user with a biz affiliation userBizEmail = '*****@*****.**' % _TEST_UUID self.userBiz = OcUser().setup_user( username=userBizEmail, email=userBizEmail, ) # set up test org self.orgTest = OcOrg().setup_org( name='test_org_existing', status='npf' ) # link org user to test org OrgUserInfo(self.userOrg.id).setup_orguser(self.orgTest) # user email used in tests self.test_email = '*****@*****.**' % _TEST_UUID Token.objects.filter(email=self.test_email).delete() # org users in tests self.test_org_name = 'test_org_%s' % _TEST_UUID
def get_amount_currents_total(self): """ returns total amount of currents in the system """ total_currents_amount = sum([ x['total'] for x in OcOrg().get_top_issued_npfs(period='all-time', quantity=int(1e6)) if x['total'] > 0 ]) return total_currents_amount
def _create_org(org_name, org_status): """ Create users and maps them to the org if needed. Takes: org_name - string org_status - string ('npf', 'biz') """ new_org = OcOrg().setup_org(name=org_name, status=org_status) return new_org
def get_biz_currents_total(self): """ returns total currents accepted in the system note: accepted = approved + pending """ currents_total = sum([ x['total'] for x in OcOrg().get_top_bizs(period='all-time', quantity=int(1e6)) if x['total'] > 0 ]) return currents_total
def setup_orgs(): orgs_all = [] for fxt in fixtures_orgs: try: org = OcOrg().setup_org(fxt['name'], fxt['status']) print 'Created org %s' % org.name except OrgExistsException: org = Org.objects.get(name=fxt['name']) print 'Org %s already exists' % fxt['name'] orgs_all.append(org) return orgs_all
def setUp(self): ''' set up test fixtures ''' # set up regular user self.userReg = OcUser().setup_user( username='******', email='*****@*****.**', ) # set up user with a npf affiliation self.userOrg = OcUser().setup_user( username='******', email='*****@*****.**', ) # set up user with a biz affiliation self.userBiz = OcUser().setup_user( username='******', email='*****@*****.**', ) # set up test org self.orgTest = OcOrg().setup_org(name='test_org_123', status='npf')
def set_up_org(self, status, old=False, user=False): org = OcOrg().setup_org(name=self._generate_test_name(prefix=status), status=status) user_org = OcUser().setup_user( username=self._generate_test_name(prefix='username'), email=self._generate_test_name(prefix='email'), ) project = Project.objects.create( org=org, name=self._generate_test_name(prefix='project') ) event = Event.objects.create( project=project, description=self._generate_test_name(prefix='desc'), location=self._generate_test_name(prefix='loc'), coordinator=user_org, creator_id=user_org.id, event_type=self._generate_test_name(prefix='event_type'), datetime_start=timezone.now() - timedelta(hours=2), datetime_end=timezone.now() - timedelta(hours=1) ) regular_user = OcUser().setup_user( username=self._generate_test_name(prefix='username'), email=self._generate_test_name(prefix='email'), ) user_reg_time_log = UserTimeLog.objects.create( user=regular_user, event=event, is_verified=True, datetime_start=timezone.now(), datetime_end=timezone.now() + timedelta(hours=1) ) regular_user_action = AdminActionUserTime.objects.create( user=user_org, usertimelog=user_reg_time_log, action_type='app' ) amount = random.randint(1, 10) if status == 'npf': OcLedger().issue_currents( org.orgentity.id, regular_user.userentity.id, regular_user_action, amount, ) elif status == 'biz': OcLedger().issue_currents( org.orgentity.id, regular_user.userentity.id, regular_user_action, amount, ) OcLedger().transact_currents( 'user', regular_user.userentity.id, 'org', org.orgentity.id, regular_user_action, amount ) if old: event_prev_month = Event.objects.last() event_prev_month.datetime_start -= timedelta(days=33) event_prev_month.save() if user: self.old_top_with_names.append({'name': regular_user.username, 'total': amount}) else: self.old_top_with_names.append({'name': org.name, 'total': amount}) self.old_top_with_names.sort(key=lambda x: x['total'], reverse=True) else: if user: self.top_with_names.append({'name': regular_user.username, 'total': amount}) else: self.top_with_names.append({'name': org.name, 'total': amount}) self.top_with_names.sort(key=lambda x: x['total'], reverse=True)
class TestOcUser(TestCase): def setUp(self): ''' set up test fixtures ''' # set up regular user self.userReg = OcUser().setup_user( username='******', email='*****@*****.**', ) # set up user with a npf affiliation self.userOrg = OcUser().setup_user( username='******', email='*****@*****.**', ) # set up user with a biz affiliation self.userBiz = OcUser().setup_user( username='******', email='*****@*****.**', ) # set up test org self.orgTest = OcOrg().setup_org(name='test_org_123', status='npf') def tearDown(self): ''' clean up test fixtures ''' self.userReg.delete() self.userOrg.delete() self.userBiz.delete() self.orgTest.delete() def test_user_exists(self): ''' tests user was created ''' user = OcUser(self.userReg.id).get_user() self.assertIsNotNone(user) def test_user_entity_exists(self): ''' tests user entity was created ''' user = OcUser(self.userReg.id) self.assertIsNotNone(user.get_user_entity()) def test_update_names(self): ''' tests first or last name are updated ''' user = OcUser(self.userReg.id) user.update_user(first_name='Jay') first_name = user.get_user().first_name self.assertEqual(first_name, 'Jay') def test_invalid_user(self): ''' check appropriate exception raised when invalid user requested ''' def _invalid_user(): user = OcUser(-1) self.assertRaises(InvalidUserException, _invalid_user) def test_user_exists(self): '''
def setUp(self): self.test_curr_per_tr = 12 # dates future_date = timezone.now() + timedelta(days=1) past_date = timezone.now() - timedelta(days=2) # creating orgs org1 = _create_org("NPF_org_1", "npf") org2 = _create_org("NPF_org_2", "npf") # creating a volunteer volunteer_1 = _create_test_user('volunteer_1') vol_1_entity = UserEntity.objects.get(user=volunteer_1) volunteer_2 = _create_test_user('volunteer_2') # creting bizorg and its admin biz_org = OcOrg().setup_org(name="BIZ_org_1", status='biz') biz_admin_1 = _create_test_user('biz_admin_1', org=biz_org, is_org_admin=True) biz_org_oc = _create_org("openCurrents", "biz") # creating an admins for NPF_orgs npf_admin_1 = _create_test_user('npf_admin_1', org=org1, is_org_admin=True) npf_admin_2 = _create_test_user('npf_admin_2', org=org2, is_org_admin=True) # creating 2 projects for 2 npf orgs project_1 = _create_project(org1, 'org1_project_1') project_2 = _create_project(org2, 'org2_project_1') # 1st event time = 3 hours datetime_start_1 = past_date datetime_end_1 = past_date + timedelta(hours=3) # 2nd event time = 2 hours datetime_start_2 = past_date + timedelta(hours=3) datetime_end_2 = past_date + timedelta(hours=5) # setting two approved events for different NPF orgs in the past # approved 3 hrs for org1 _setup_volunteer_hours(volunteer_1, npf_admin_1, org1, project_1, datetime_start_1, datetime_end_1, is_verified=True, action_type='app') # approved 2 hrs for org2 _setup_volunteer_hours(volunteer_1, npf_admin_2, org2, project_2, datetime_start_2, datetime_end_2, is_verified=True, action_type='app') # setting a pending past events 3 hrs _setup_volunteer_hours(volunteer_1, npf_admin_1, org1, project_1, datetime_start_1, datetime_end_1) # setting up two future events 5 hrs each self.event1 = _create_event(project_1, npf_admin_1.id, future_date, future_date + timedelta(hours=5), is_public=True) self.event2 = _create_event(project_1, npf_admin_1.id, future_date, future_date + timedelta(hours=5), description="Test Event 2", is_public=True) # issuing currents to volunteer1 OcLedger().issue_currents( entity_id_from=org1.orgentity.id, entity_id_to=vol_1_entity.id, action=None, amount=40, ) # registering user for an event _setup_user_event_registration(volunteer_1, self.event2) # setting up user dollars "Dollars available" _setup_ledger_entry(org1.orgentity, vol_1_entity, currency='usd', amount=30.30, is_issued=True) # setting approved and pending dollars _setup_transactions(biz_org, volunteer_1, self.test_curr_per_tr, 20) _setup_transactions(biz_org, volunteer_1, self.test_curr_per_tr, 20, action_type='app') _setup_transactions(biz_org, volunteer_1, self.test_curr_per_tr, 20, action_type='red') # create master offer _create_offer(biz_org, currents_share=_SHARE * 100, is_master=True) # setting up client self.client = Client()
def set_up_objects(self, old=False, user=False): # creating NPF org org = OcOrg().setup_org(name="NPF_org_1", status="npf") self.org_id = org_id = org.id # create BIZ org biz_org = _create_org("BIZ_org_1", 'biz') # create master offer _create_offer(biz_org, currents_share=_SHARE * 100, is_master=True) # creating users # admins self.create_user('org_user_1', org, is_org_user=True, is_org_admin=True) self.create_user('org_user_2', org, is_org_user=True, is_org_admin=True, password='******') self.create_user('org_user_3', org, is_org_user=True, is_org_admin=True, password='******') self.org_user_1 = User.objects.get(username='******') self.org_user_2 = User.objects.get(username='******') self.org_user_3 = User.objects.get(username='******') # volunteer user self.create_user('volunteer_1', org, is_org_user=False, is_org_admin=False) self.create_user('volunteer_2', org, is_org_user=False, is_org_admin=False) # setup Project project = Project.objects.create(org=org, name="test_project_1") org_admin = User.objects.get(username='******') future_date = timezone.now() + timedelta(days=1) past_date = timezone.now() - timedelta(days=2) # setting up events self.future_event = Event.objects.create(project=project, is_public=True, description="future event", location="test_location_1", coordinator=org_admin, creator_id=org_admin.id, event_type="GR", datetime_start=future_date, datetime_end=future_date + timedelta(days=1)) self.current_event = Event.objects.create(project=project, is_public=True, description="current event", location="test_location_2", coordinator=org_admin, creator_id=org_admin.id, event_type="GR", datetime_start=past_date, datetime_end=future_date) self.finished_event = Event.objects.create( project=project, is_public=True, description="finished event", location="test_location_3", coordinator=org_admin, creator_id=org_admin.id, event_type="GR", datetime_start=past_date, datetime_end=past_date + timedelta(days=1)) # @@ TODO @@ # move repeatable actions during setting up testing environment to a class in tests/interfaces # # creating APPROVED 4 hours for NPF admin1 volunteer1 = User.objects.get(username='******') datetime_start = past_date + timedelta(hours=2) datetime_end = past_date + timedelta(hours=6) project = Project.objects.create(org=org, name="test_project_2") volunteer1_mt_event_1 = Event.objects.create( project=project, is_public=True, description="finished event", location="test_location_4", coordinator=org_admin, event_type="MN", datetime_start=datetime_start, datetime_end=datetime_end) volunteer1_timelog = UserTimeLog.objects.create( user=volunteer1, event=volunteer1_mt_event_1, datetime_start=datetime_start, datetime_end=datetime_end, is_verified=True) actiontimelog = AdminActionUserTime.objects.create( user=org_admin, usertimelog=volunteer1_timelog, action_type='app') # creating APPROVED 2 hours for NPF admin2 org_admin = User.objects.get(username='******') volunteer2 = User.objects.get(username='******') datetime_start = past_date + timedelta(hours=1) datetime_end = past_date + timedelta(hours=3) project = Project.objects.create(org=org, name="test_project_2") volunteer2_mt_event_1 = Event.objects.create( project=project, is_public=True, description="finished event 2", location="test_location_5", coordinator=org_admin, event_type="MN", datetime_start=datetime_start, datetime_end=datetime_end) volunteer2_timelog = UserTimeLog.objects.create( user=volunteer2, event=volunteer2_mt_event_1, datetime_start=datetime_start, datetime_end=datetime_end, is_verified=True) actiontimelog = AdminActionUserTime.objects.create( user=org_admin, usertimelog=volunteer2_timelog, action_type='app') # creating APPROVED 6 hours for NPF admin3 org_admin = User.objects.get(username='******') volunteer2 = User.objects.get(username='******') datetime_start = past_date + timedelta(hours=4) datetime_end = past_date + timedelta(hours=10) project = Project.objects.create(org=org, name="test_project_2") volunteer2_mt_event_1 = Event.objects.create( project=project, is_public=True, description="finished event 2", location="test_location_5", coordinator=org_admin, event_type="MN", datetime_start=datetime_start, datetime_end=datetime_end) volunteer2_timelog = UserTimeLog.objects.create( user=volunteer2, event=volunteer2_mt_event_1, datetime_start=datetime_start, datetime_end=datetime_end, is_verified=True) actiontimelog = AdminActionUserTime.objects.create( user=org_admin, usertimelog=volunteer2_timelog, action_type='app') # creating PENDING 1 hour assigned to NPF admin1 (currently logged in) volunteer2 = User.objects.get(username='******') org_admin = User.objects.get(username='******') datetime_start = past_date + timedelta(hours=5) datetime_end = past_date + timedelta(hours=6) volunteer2_mt_event_2 = Event.objects.create( project=project, is_public=True, description="pending event", location="test_location_6", coordinator=org_admin, event_type="MN", datetime_start=datetime_start, datetime_end=datetime_end) volunteer2_timelog = UserTimeLog.objects.create( user=volunteer2, event=volunteer2_mt_event_2, datetime_start=datetime_start, datetime_end=datetime_end, is_verified=False) actiontimelog = AdminActionUserTime.objects.create( user=org_admin, usertimelog=volunteer2_timelog, action_type='req') # creating PENDING 2 hours assigned to NPF admin2 volunteer1 = User.objects.get(username='******') org_admin = User.objects.get(username='******') datetime_start = past_date + timedelta(hours=2) datetime_end = past_date + timedelta(hours=4) volunteer1_mt_event_2 = Event.objects.create( project=project, is_public=True, description="pending event", location="test_location_7", coordinator=org_admin, event_type="MN", datetime_start=datetime_start, datetime_end=datetime_end) volunteer1_timelog = UserTimeLog.objects.create( user=volunteer1, event=volunteer1_mt_event_2, datetime_start=datetime_start, datetime_end=datetime_end, is_verified=False) actiontimelog = AdminActionUserTime.objects.create( user=org_admin, usertimelog=volunteer1_timelog, action_type='req') # creating PENDING 3 hours assigned to NPF admin3 volunteer2 = User.objects.get(username='******') org_admin = User.objects.get(username='******') datetime_start = past_date + timedelta(hours=1) datetime_end = past_date + timedelta(hours=4) volunteer2_mt_event_2 = Event.objects.create( project=project, is_public=True, description="pending event", location="test_location_8", coordinator=org_admin, event_type="MN", datetime_start=datetime_start, datetime_end=datetime_end) volunteer2_timelog = UserTimeLog.objects.create( user=volunteer2, event=volunteer2_mt_event_2, datetime_start=datetime_start, datetime_end=datetime_end, is_verified=False) actiontimelog = AdminActionUserTime.objects.create( user=org_admin, usertimelog=volunteer2_timelog, action_type='req')
class TestOcLedger(TestCase): def setUp(self): ''' set up test fixtures ''' self.ledger = OcLedger() # set up regular user self.userReg = OcUser().setup_user( username='******', email='*****@*****.**', ) # set up user with a npf affiliation self.userOrg = OcUser().setup_user( username='******', email='*****@*****.**', ) # set up user with a biz affiliation self.userBiz = OcUser().setup_user( username='******', email='*****@*****.**', ) # set up test org self.orgTest = OcOrg().setup_org(name='test_org_123', status='npf') self.projectUserRegMT = Project(org=self.orgTest, name='MT by userReg') self.projectUserRegMT.save() self.eventUserRegMT = Event( project=self.projectUserRegMT, description='MT by userReg', location='test', coordinator=self.userOrg, creator_id=self.userOrg.id, event_type='MN', datetime_start=datetime.now(tz=pytz.utc) - timedelta(hours=2), datetime_end=datetime.now(tz=pytz.utc) - timedelta(hours=1)) self.eventUserRegMT.save() self.userRegTimeLog = UserTimeLog( user=self.userReg, event=self.eventUserRegMT, is_verified=True, datetime_start=datetime.now(tz=pytz.utc), datetime_end=datetime.now(tz=pytz.utc) + timedelta(hours=1)) self.userRegTimeLog.save() self.actionUserReg = AdminActionUserTime( user=self.userOrg, usertimelog=self.userRegTimeLog, action_type='app') self.actionUserReg.save() # issue currents to userReg self.ledger.issue_currents(self.orgTest.orgentity.id, self.userReg.userentity.id, self.actionUserReg, 1) def tearDown(self): ''' clean up test fixtures ''' self.userReg.delete() self.userOrg.delete() self.userBiz.delete() self.orgTest.delete() Ledger.objects.filter(entity_from__id__in=[ self.userReg.userentity.id, self.userOrg.userentity.id, self.userBiz.userentity.id, self.orgTest.orgentity.id ]).delete() def test_initial_balance(self): ''' test initial balances correct after set up ''' self.assertEqual(self.ledger.get_balance(self.userReg.userentity.id), 1) self.assertEqual(self.ledger.get_balance(self.userOrg.userentity.id), 0) # issued currents not counted towards balances self.assertEqual( self.ledger.get_balance(self.orgTest.orgentity.id, 'org'), 0) def test_transact_user_user(self): ''' transact from one user to another and check balances ''' self.ledger.transact_currents( entity_type_from='user', entity_id_from=self.userReg.userentity.id, entity_type_to='user', entity_id_to=self.userBiz.userentity.id, action=self.actionUserReg, amount=1, ) self.assertEqual(self.ledger.get_balance(self.userReg.userentity.id), 0) self.assertEqual(self.ledger.get_balance(self.userBiz.userentity.id), 1) def test_insufficient_funds(self): ''' check appropriate exception raised when sender has insufficient funds ''' def _insufficient_funds_transaction(): self.ledger.transact_currents( entity_type_from='user', entity_id_from=self.userReg.userentity.id, entity_type_to='user', entity_id_to=self.userOrg.userentity.id, action=self.actionUserReg, amount=100) self.assertRaises(InsufficientFundsException, _insufficient_funds_transaction)
class TestSignup(TransactionTestCase): """Main test class.""" def setUp(self): """Set up test fixtures.""" _TEST_UUID = uuid.uuid4() self.client = Client() # set up regular user userRegEmail = '*****@*****.**' % _TEST_UUID self.userReg = OcUser().setup_user( username=userRegEmail, email=userRegEmail, ) # set up user with a npf affiliation userOrgEmail = '*****@*****.**' % _TEST_UUID self.userOrg = OcUser().setup_user( username=userOrgEmail, email=userOrgEmail, ) # set up user with a biz affiliation userBizEmail = '*****@*****.**' % _TEST_UUID self.userBiz = OcUser().setup_user( username=userBizEmail, email=userBizEmail, ) # set up test org self.orgTest = OcOrg().setup_org( name='test_org_existing', status='npf' ) # link org user to test org OrgUserInfo(self.userOrg.id).setup_orguser(self.orgTest) # user email used in tests self.test_email = '*****@*****.**' % _TEST_UUID Token.objects.filter(email=self.test_email).delete() # org users in tests self.test_org_name = 'test_org_%s' % _TEST_UUID def tearDown(self): """Clear up test fixtures.""" self.userReg.delete() self.userOrg.delete() self.userBiz.delete() self.orgTest.delete() User.objects.filter(username=self.test_email).delete() Token.objects.filter(email=self.test_email).delete() org = None try: org = Org.objects.get(name=self.test_org_name) except Exception: pass # delete org admin group if org: Group.objects.filter(name='admin_%s' % org.name).delete() Org.objects.filter(name=self.test_org_name).delete() def _assert_user(self, username, is_true): ''' assert user exists ''' users = User.objects.filter(username=username) does_exist = users.exists() if is_true: self.assertTrue(does_exist) return users[0] else: self.assertFalse(does_exist) return None def _assert_user_has_usable_password(self, username, is_true): ''' assert user exists and has a password set ''' try: user = User.objects.get(username=username) except User.DoesNotExist: self.assertFalse(is_true) has_usable_password = user.has_usable_password() if is_true: self.assertTrue(has_usable_password) else: self.assertFalse(has_usable_password) def _assert_num_users(self, user_email, num_users): ''' assert number of user for a given email ''' users = User.objects.filter(email=user_email) self.assertEqual(len(users), num_users) def _assert_token_valid(self, username, is_true): ''' assert valid verification token was generated - unverified - type signup - timestamp recent enough ''' nowish = datetime.now(tz=pytz.utc) - timedelta(seconds=3) tokens = Token.objects.filter( email=username, is_verified=False, token_type='signup', date_created__gte=nowish, date_expires__gte=nowish + timedelta(days=6), date_expires__lte=nowish + timedelta(days=8) ) if is_true: self.assertTrue(tokens.exists()) else: self.assertFalse(tokens.exists()) def _assert_org(self, org_name, org_status, is_true): ''' assert org exists and is unique ''' orgs = Org.objects.filter(name=org_name) if is_true: self.assertTrue(orgs.exists()) self.assertEqual(len(orgs), 1) self.assertEqual(orgs[0].status, org_status) return orgs[0] else: self.assertFalse(orgs.exists()) def _assert_org_user(self, org_name, user_email, is_true): ''' assert orguser association exists and is unique ''' orgusers = OrgUser.objects.filter( org__name=org_name, user__email=user_email ) if is_true: self.assertTrue(orgusers.exists()) self.assertEqual(len(orgusers), 1) else: self.assertFalse(orgusers.exists()) def _assert_group(self, org_name, is_true): ''' assert empty user group exists and is unique ''' org = None try: org = Org.objects.get(name=org_name) except Exception as e: pass if is_true: self.assertTrue(org) else: self.assertFalse(org) groups = Group.objects.filter(name='admin_%s' % org.id) if is_true: self.assertTrue(groups.exists()) self.assertEqual(len(groups), 1) if org.status == 'biz': self.assertEqual(len(groups[0].user_set.all()), 1) else: self.assertFalse(groups[0].user_set.all()) return groups[0] else: self.assertFalse(groups.exists()) def test_signup_user_new(self): ''' test signup successful for a new user - user should be created - user should have no password set - token should have been generated - redirected to 'check-email' ''' self._assert_user(self.test_email, False) self._assert_token_valid(self.test_email, False) response = self.client.post( testing_urls.url_signup, data={ 'user_email': self.test_email, 'user_firstname': 'test_firstname', 'user_lastname': 'test_lastname', 'signup_status': 'vol' } ) self._assert_user(self.test_email, True) self._assert_user_has_usable_password(self.test_email, False) self._assert_token_valid(self.test_email, True) url_check_email = reverse( 'check-email', urlconf=urls, kwargs={'user_email': self.test_email} ) self.assertRedirects(response, url_check_email) def test_signup_user_existing_no_password(self): ''' tests signup successful for existing user without password - new user not created - existing user should be used - user should have no password set - token should have been generated - redirected to 'check-email' ''' self._assert_user(self.userReg.username, True) self._assert_num_users(self.userReg.email, 1) self._assert_user_has_usable_password(self.userReg.username, False) response = self.client.post( testing_urls.url_signup, data={ 'user_email': self.userReg.email, 'user_firstname': 'test_firstname', 'user_lastname': 'test_lastname', 'signup_status': 'vol' } ) user = self._assert_user(self.userReg.email, True) self._assert_num_users(self.userReg.email, 1) self._assert_user_has_usable_password(self.userReg.email, False) self._assert_token_valid(self.userReg.email, True) # check first and last name were updated self.assertEqual(user.first_name, 'test_firstname') self.assertEqual(user.last_name, 'test_lastname') url_check_email = reverse( 'check-email', urlconf=urls, kwargs={'user_email': self.userReg.email} ) self.assertRedirects(response, url_check_email) def test_signup_user_existing_with_password(self): ''' tests signup successful for existing user with password - new user not created - existing user should be used - token not generated - redirected to 'login' ''' self._assert_user(self.userReg.username, True) self._assert_user_has_usable_password(self.userReg.username, False) self._assert_num_users(self.userReg.email, 1) self._assert_token_valid(self.userReg.email, False) # set password for user self.userReg.set_password(uuid.uuid4()) self.userReg.save() self._assert_user_has_usable_password(self.userReg.username, True) response = self.client.post( testing_urls.url_signup, data={ 'user_email': self.userReg.email, 'user_firstname': 'test_firstname', 'user_lastname': 'test_lastname', 'signup_status': 'vol' } ) self._assert_num_users(self.userReg.email, 1) self._assert_token_valid(self.userReg.email, False) url_login = reverse( 'login', urlconf=urls, kwargs={ 'status_msg': 'User with this email already exists', 'msg_type': 'alert' } ) self.assertRedirects(response, url_login) def test_signup_user_org_npf_new(self): ''' tests signup successful for new org user - new user created - token generated - org created - org user created - org admin group created - redirected to 'check-email' with org id ''' self._assert_user(self.test_email, False) response = self.client.post( testing_urls.url_signup, data={ 'user_email': self.test_email, 'user_firstname': 'test_firstname', 'user_lastname': 'test_lastname', 'npf_name': self.test_org_name, 'signup_status': 'npf' } ) self._assert_user(self.test_email, True) self._assert_user_has_usable_password(self.test_email, False) self._assert_token_valid(self.test_email, True) org = self._assert_org(self.test_org_name, 'npf', True) self._assert_org_user(self.test_org_name, self.test_email, True) self._assert_group(self.test_org_name, True) url_login = reverse( 'check-email', urlconf=urls, kwargs={ 'user_email': self.test_email, 'orgid': org.id } ) self.assertRedirects(response, url_login) def test_signup_user_org_biz_new(self): ''' tests signup successful for new biz user - new user created - token generated - biz org created - biz org user created - biz admin group created - redirected to 'check-email' with org id ''' self._assert_user(self.test_email, False) response = self.client.post( testing_urls.url_signup, data={ 'user_email': self.test_email, 'user_firstname': 'test_firstname', 'user_lastname': 'test_lastname', 'biz_name': self.test_org_name, 'signup_status': 'biz' } ) self._assert_user(self.test_email, True) self._assert_user_has_usable_password(self.test_email, False) self._assert_token_valid(self.test_email, True) org = self._assert_org(self.test_org_name, 'biz', True) self._assert_org_user(self.test_org_name, self.test_email, True) self._assert_group(self.test_org_name, True) # url_login = reverse( # 'check-email', # urlconf=urls, # kwargs={ # 'user_email': self.test_email, # 'orgid': org.id # } # ) url_redirect = reverse( 'offer', urlconf=urls, ) self.assertRedirects(response, url_redirect) @skip('Fix (#1567)') def test_signup_user_org_existing(self): ''' tests signup fails for invalid org - new user created - token generated - biz org not created - biz org user not created - biz admin group not created - redirected to 'nonprofit' with proper status message ''' self._assert_user(self.test_email, False) self._assert_org(self.orgTest.name, 'npf', True) response = self.client.post( testing_urls.url_signup, data={ 'user_email': self.test_email, 'user_firstname': 'test_firstname', 'user_lastname': 'test_lastname', 'npf_name': self.orgTest.name, 'signup_status': 'npf' } ) self._assert_user(self.test_email, False) # we don't create a user if org exists self._assert_token_valid(self.test_email, False) self._assert_org_user(self.orgTest.name, self.test_email, False) status_message = 'Organization named %s already exists!' % self.orgTest.name warning_messages = list(response.wsgi_request._messages) self.assertEqual(warning_messages[0].message, status_message) self.assertIn('alert', warning_messages[0].tags) url_nonprofit = reverse( 'home', urlconf=urls, ) + '#signup' self.assertRedirects(response, url_nonprofit) def test_signup_livedashboard_optin(self): ''' test signup from live dashboard with optin - user created - user no password set - token not generated - response status code 201 with user id ''' self._assert_user(self.test_email, False) self._assert_token_valid(self.test_email, False) response = self.client.post( testing_urls.url_signup_endpoint, data={ 'user_email': self.test_email, 'user_firstname': 'test_firstname', 'user_lastname': 'test_lastname', 'org_admin_id': self.userOrg.id, 'signup_status': 'vol' } ) user = self._assert_user(self.test_email, True) self._assert_user_has_usable_password(self.test_email, False) self._assert_token_valid(self.test_email, False) self.assertEqual(int(response.status_code), 201) self.assertEqual(int(response.content), user.id)
def setUp(self): """Set testing environment.""" # creating org self.org1 = OcOrg().setup_org(name="NPF_org_1", status="npf") # creating volunteers self.volunteer_1 = _create_test_user('volunteer_1') self.volunteer_2 = _create_test_user('volunteer_2') # getting full names of volunteers self.volunteer_1_full_name = self.volunteer_1.first_name + ' ' + self.volunteer_1.last_name self.volunteer_2_full_name = self.volunteer_2.first_name + ' ' + self.volunteer_2.last_name # creating an admins for NPF_orgs self.npf_admin_1 = _create_test_user('npf_admin_1', org=self.org1, is_org_admin=True) # creating a project self.project_1 = _create_project(self.org1, 'org1_project_1') # 1st event time = 3 hours datetime_start_1 = timezone.now() - timedelta(hours=4) datetime_end_1 = datetime_start_1 + timedelta(hours=3) # 2nd event time = 2 hours datetime_start_2 = timezone.now() - timedelta(hours=4) datetime_end_2 = datetime_start_2 + timedelta(hours=2) # setting 2 pending events _setup_volunteer_hours(self.volunteer_1, self.npf_admin_1, self.org1, self.project_1, datetime_start_1, datetime_end_1) _setup_volunteer_hours( self.volunteer_2, self.npf_admin_1, self.org1, self.project_1, datetime_start_2, datetime_end_2, ) # getting previous week start self.monday = ( timezone.now() - timedelta(days=timezone.now().weekday())).strftime("%-m-%-d-%Y") # oc instances self.oc_vol_1 = OcUser(self.volunteer_1.id) self.oc_vol_2 = OcUser(self.volunteer_2.id) # user entities self.user_enitity_id_vol_1 = UserEntity.objects.get( user=self.volunteer_1).id self.user_enitity_id_vol_2 = UserEntity.objects.get( user=self.volunteer_2).id # setting up client self.client = Client() self.client.login(username=self.npf_admin_1.username, password='******') self.response = self.client.get('/approve-hours/')