def setUp(self): self.url = '/api/stores/' # group with two members and one store self.member = UserFactory() self.member2 = UserFactory() self.group = GroupFactory(members=[self.member, self.member2]) self.store = StoreFactory(group=self.group) self.store_url = self.url + str(self.store.id) + '/' # not a member self.user = UserFactory() # another store for above group self.store_data = { 'name': faker.name(), 'description': faker.name(), 'group': self.group.id, 'address': faker.address(), 'latitude': faker.latitude(), 'longitude': faker.longitude() } # another group self.different_group = GroupFactory(members=[self.member2])
def setUpBeforeMigration(self, apps): User = apps.get_model('users', 'User') Group = apps.get_model('groups', 'Group') Store = apps.get_model('stores', 'Store') PickupDateSeries = apps.get_model('stores', 'PickupDateSeries') PickupDate = apps.get_model('stores', 'PickupDate') Feedback = apps.get_model('stores', 'Feedback') self.email = faker.email() self.now = datetime.datetime.now(tz=pytz.utc) self.date = faker.date_time_between(start_date='now', end_date='+24h', tzinfo=pytz.utc) self.group_name = 'Group ' + faker.name() self.store_name = 'Store ' + faker.name() user = User.objects.create(email=self.email, display_name='Peter') group = Group.objects.create(name=self.group_name) store = Store.objects.create(name=self.store_name, group=group) pickup_date_series = PickupDateSeries.objects.create( store=store, start_date=self.now) pickup_date = PickupDate.objects.create(series=pickup_date_series, store=store, date=self.date) pickup_date.collectors.add(user) Feedback.objects.create(given_by=user, about=pickup_date)
def setUpClass(cls): super().setUpClass() cls.url = '/api/stores/' # group with two members and one store cls.member = UserFactory() cls.member2 = UserFactory() cls.group = GroupFactory(members=[cls.member, cls.member2]) cls.store = StoreFactory(group=cls.group) cls.store_url = cls.url + str(cls.store.id) + '/' # not a member cls.user = UserFactory() # another store for above group cls.store_data = { 'name': faker.name(), 'description': faker.name(), 'group': cls.group.id, 'address': faker.address(), 'latitude': faker.latitude(), 'longitude': faker.longitude() } # another group cls.different_group = GroupFactory(members=[ cls.member2, ])
def test_sign_up_with_rejected_address_fails(self): response = self.client.post(self.url, { 'email': '*****@*****.**', 'password': faker.name(), 'display_name': faker.name() }) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST, response.data)
def setUp(self): self.user = UserFactory() self.url = '/api/auth/user/' self.user_data = { 'email': faker.email(), 'password': faker.name(), 'display_name': faker.name(), 'address': faker.address(), 'latitude': faker.latitude(), 'longitude': faker.longitude() } mail.outbox = []
def test_create_user_with_similar_cased_email_fails(self): response = self.client.post(self.url, { 'email': '*****@*****.**', 'password': faker.name(), 'display_name': faker.name() }) self.assertEqual(response.status_code, status.HTTP_201_CREATED) response = self.client.post(self.url, { 'email': '*****@*****.**', 'password': faker.name(), 'display_name': faker.name() }) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST, response.data) self.assertEqual(response.data['email'], ['Similar e-mail exists: [email protected]'])
class StoreFactory(DjangoModelFactory): class Meta: model = StoreModel group = SubFactory(GroupFactory) name = LazyAttribute(lambda x: faker.sentence(nb_words=4)) description = LazyAttribute(lambda x: faker.name())
def test_group_creator_is_initial_membership_manager(self): self.client.force_login(user=self.creator) response = self.client.post('/api/groups/', { 'name': faker.name(), 'timezone': 'Europe/Berlin' }) self.assertEqual(response.status_code, status.HTTP_201_CREATED) group_id = response.data['id'] membership = GroupMembership.objects.get(group=group_id, user=self.creator) role = roles.GROUP_MEMBERSHIP_MANAGER self.assertIn(role, membership.roles) # can't drop management rights as only admin self.client.delete('/api/groups/{}/users/{}/roles/{}/'.format( group_id, self.creator.id, role)) membership.refresh_from_db() self.assertIn(role, membership.roles) # creator makes another person admin and drops own rights GroupModel.objects.get(id=group_id).add_member(self.member) response = self.client.put('/api/groups/{}/users/{}/roles/{}/'.format( group_id, self.member.id, role)) self.assertEqual(response.status_code, status.HTTP_200_OK) self.client.delete('/api/groups/{}/users/{}/roles/{}/'.format( group_id, self.creator.id, role)) membership.refresh_from_db() self.assertNotIn(role, membership.roles)
def test_unrelated_user_receives_no_changes(self): self.client = self.connect_as(self.unrelated_user) self.member.display_name = faker.name() self.member.save() self.assertEqual(len(self.client.messages), 0)
def modify_store(store): data = c.patch('/api/stores/{}/'.format(store), { 'name': 'Store (edited) ' + faker.name(), 'description': faker.text(), }).data print('modified store: ', store) return data
def setUpBeforeMigration(self, apps): logging.disable(logging.CRITICAL) self.email = faker.email() User = apps.get_model('users', 'User') History = apps.get_model('history', 'History') Group = apps.get_model('groups', 'Group') GroupMembership = apps.get_model('groups', 'GroupMembership') self.user = User.objects.create(email=self.email, display_name='Peter') self.group = Group.objects.create( name=faker.name(), description=faker.sentence(nb_words=40), public_description=faker.sentence(nb_words=20), ) membership = GroupMembership.objects.create(group=self.group, user=self.user) self.membership_id = membership.id self.history = History.objects.create( typus=HistoryTypus.GROUP_CREATE, group=self.group, payload={}, ) self.history.users.add(self.user) self.history.save() self.history.date = timezone.now() - relativedelta(days=100) self.history.save() self.user.created_at = timezone.now() - relativedelta(days=200) self.user.save()
def test_unrelated_user_receives_no_changes(self): self.client.force_login(self.unrelated_user) self.client.send_and_consume('websocket.connect', path='/') self.member.display_name = faker.name() self.member.save() self.assertIsNone(self.client.receive(json=True))
def test_normal_member_cannot_create_agreement(self): self.client.force_login(user=self.normal_member) response = self.client.post('/api/agreements/', { 'title': faker.name(), 'content': faker.text(), 'group': self.group.id }) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def setUpClass(cls): super().setUpClass() cls.user = UserFactory() cls.user2 = UserFactory() cls.verified_user = VerifiedUserFactory() cls.url = '/api/users/' cls.user_data = { 'email': faker.email(), 'password': faker.name(), 'display_name': faker.name(), 'address': faker.address(), 'latitude': faker.latitude(), 'longitude': faker.longitude() } cls.group = GroupFactory(members=[cls.user, cls.user2]) cls.another_common_group = GroupFactory(members=[cls.user, cls.user2]) cls.user_in_another_group = UserFactory() cls.another_group = GroupFactory(members=[cls.user_in_another_group, ])
def make_user(): data = c.post('/api/auth/user/', { 'email': str(timezone.now().microsecond) + faker.email(), 'password': password, 'display_name': faker.name(), 'description': 'I am a fake user', 'mobile_number': faker.phone_number() }).data print('created user:'******'email']) return data
def test_receive_store_changes(self): self.client = self.connect_as(self.member) name = faker.name() self.store.name = name self.store.save() response = self.client.messages_by_topic.get('stores:store')[0] self.assertEqual(response['payload']['name'], name) self.assertEqual(len(self.client.messages), 1)
def setUp(self): self.user = UserFactory() self.user2 = UserFactory() self.url = '/api/users/' self.user_data = { 'email': faker.email(), 'password': faker.name(), 'display_name': faker.name(), 'address': faker.address(), 'latitude': faker.latitude(), 'longitude': faker.longitude() } self.group = GroupFactory(members=[self.user, self.user2]) self.another_common_group = GroupFactory( members=[self.user, self.user2]) self.user_in_another_group = UserFactory() self.another_group = GroupFactory(members=[ self.user_in_another_group, ]) mail.outbox = []
def test_do_not_send_too_many_updates(self): [GroupFactory(members=[self.member, self.other_member]) for _ in range(3)] self.client = self.connect_as(self.member) name = faker.name() self.other_member.display_name = name self.other_member.save() self.assertEqual(len(self.client.messages), 1) self.assertIn('users:user', self.client.messages_by_topic.keys())
def make_store(group): data = c.post('/api/stores/', { 'name': 'Store ' + faker.name(), 'description': faker.text(), 'group': group, 'address': faker.street_address(), 'latitude': faker.latitude(), 'longitude': faker.longitude(), 'status': 'active' }).data print('created store: ', data['id'], data['name']) return data
def test_receive_group_changes_as_nonmember(self): self.client = self.connect_as(self.user) name = faker.name() self.group.name = name self.group.save() response = self.client.messages_by_topic.get('groups:group_preview')[0] self.assertEqual(response['payload']['name'], name) self.assertTrue('description' not in response['payload']) self.assertEqual(len(self.client.messages), 1)
def test_receive_store_changes(self): self.client.force_login(self.member) self.client.send_and_consume('websocket.connect', path='/') name = faker.name() self.store.name = name self.store.save() response = self.client.receive(json=True) self.assertEqual(response['topic'], 'stores:store') self.assertEqual(response['payload']['name'], name) self.assertIsNone(self.client.receive(json=True))
def setUp(self): self.user = UserFactory() self.member = UserFactory() self.group = GroupFactory(members=[self.member, ]) self.group_with_password = GroupFactory(password='******') self.join_password_url = '/api/groups/{}/join/'.format(self.group_with_password.id) self.url = '/api/groups/' self.group_data = {'name': faker.name(), 'description': faker.text(), 'address': faker.address(), 'latitude': faker.latitude(), 'longitude': faker.longitude(), 'timezone': 'Europe/Berlin'}
def test_receive_changes_of_other_user(self): self.client = self.connect_as(self.member) name = faker.name() self.other_member.display_name = name self.other_member.save() response = self.client.messages_by_topic.get('users:user')[0] self.assertEqual(response['payload']['display_name'], name) self.assertTrue('current_group' not in response['payload']) self.assertTrue(response['payload']['photo_urls']['full_size'].startswith(settings.HOSTNAME)) self.assertEqual(len(self.client.messages), 1)
def setUpBeforeMigration(self, apps): Group = apps.get_model('groups', 'Group') Store = apps.get_model('stores', 'Store') PickupDateSeries = apps.get_model('pickups', 'PickupDateSeries') PickupDate = apps.get_model('pickups', 'PickupDate') History = apps.get_model('history', 'History') # upcoming pickup is moved to a later date date1 = faker.date_time_between(start_date='+1h', end_date='+24h', tzinfo=pytz.utc) date2 = faker.date_time_between(start_date='+24h', end_date='+48h', tzinfo=pytz.utc) group = Group.objects.create(name=faker.name()) store = Store.objects.create(name=faker.name(), group=group) pickup_date_series = PickupDateSeries.objects.create( store=store, start_date=timezone.now()) pickup_date = PickupDate.objects.create(series=pickup_date_series, store=store, date=date2, is_date_changed=True) # PICKUP_MODIFY history entry History.objects.create(typus=8, group=group, before={ 'id': pickup_date.id, 'is_date_changed': False, 'date': date1.isoformat() }, after={'is_date_changed': True}) # past moved pickup PickupDate.objects.create(series=pickup_date_series, store=store, date=timezone.now() - relativedelta(days=1), is_date_changed=True)
def setUpClass(cls): super().setUpClass() cls.user = UserFactory() cls.member = UserFactory() cls.group = GroupFactory(members=[cls.member, ]) cls.group_with_password = GroupFactory(password='******') cls.join_password_url = '/api/groups/{}/join/'.format(cls.group_with_password.id) cls.url = '/api/groups/' cls.group_data = {'name': faker.name(), 'description': faker.text(), 'address': faker.address(), 'latitude': faker.latitude(), 'longitude': faker.longitude(), 'timezone': 'Europe/Berlin'}
def test_receive_own_user_changes(self): self.client = self.connect_as(self.member) name = faker.name() self.member.display_name = name self.member.save() response = self.client.messages[0] self.assertEqual(response['topic'], 'auth:user') self.assertEqual(response['payload']['display_name'], name) self.assertTrue('current_group' in response['payload']) self.assertTrue(response['payload']['photo_urls']['full_size'].startswith(settings.HOSTNAME)) self.assertEqual(len(self.client.messages), 1)
def test_receive_group_changes_as_nonmember(self): self.client.force_login(self.user) self.client.send_and_consume('websocket.connect', path='/') name = faker.name() self.group.name = name self.group.save() response = self.client.receive(json=True) self.assertEqual(response['topic'], 'groups:group_preview') self.assertEqual(response['payload']['name'], name) self.assertTrue('description' not in response['payload']) self.assertIsNone(self.client.receive(json=True))
class GroupFactory(DjangoModelFactory): class Meta: model = GroupModel @post_generation def members(self, created, members, **kwargs): if not created: return if members: for member in members: self.members.add(member) name = LazyAttribute(lambda x: faker.sentence(nb_words=4)) description = LazyAttribute(lambda x: faker.name()) public_description = LazyAttribute(lambda x: faker.sentence(nb_words=20))
def test_receive_changes_of_other_user(self): self.client.force_login(self.member) self.client.send_and_consume('websocket.connect', path='/') name = faker.name() self.other_member.display_name = name self.other_member.save() response = self.client.receive(json=True) self.assertEqual(response['topic'], 'users:user') self.assertEqual(response['payload']['display_name'], name) self.assertTrue('current_group' not in response['payload']) self.assertTrue(response['payload']['photo_urls']['full_size'].startswith(settings.HOSTNAME)) self.assertIsNone(self.client.receive(json=True))
class GroupFactory(DjangoModelFactory): class Meta: model = GroupModel @post_generation def members(self, created, members, **kwargs): if not created: return if members: for member in members: GroupMembership.objects.create(group=self, user=member) name = LazyAttribute(lambda x: faker.name()) description = LazyAttribute(lambda x: faker.sentence(nb_words=40)) public_description = LazyAttribute(lambda x: faker.sentence(nb_words=20)) application_questions = LazyAttribute( lambda x: faker.sentence(nb_words=20))