def test_can_create_agreement(self): self.client.force_login(user=self.agreement_manager) response = self.client.post('/api/agreements/', { 'title': faker.text(), 'content': faker.text(), 'group': self.group.id }) self.assertEqual(response.status_code, status.HTTP_201_CREATED)
def test_cannot_create_agreement_for_another_group(self): self.client.force_login(user=self.agreement_manager) response = self.client.post( '/api/agreements/', { 'title': faker.text(), 'content': faker.text(), 'group': self.other_group.id }) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def setUp(self): self.normal_member = UserFactory() self.agreement_manager = UserFactory() self.group = GroupFactory(members=[self.normal_member, self.agreement_manager, ]) self.agreement = Agreement.objects.create(group=self.group, title=faker.text(), content=faker.text()) membership = GroupMembership.objects.get(group=self.group, user=self.agreement_manager) membership.roles.append(roles.GROUP_AGREEMENT_MANAGER) membership.save() # other group/agreement that neither user is part of self.other_group = GroupFactory() self.other_agreement = Agreement.objects.create(group=self.other_group, title=faker.text(), content=faker.text())
def modify_group(group): data = c.patch('/api/groups/{}/'.format(group), { 'name': 'Group (edited) ' + faker.city(), 'description': faker.text(), }).data print('modified group: ', group) return data
class Message(DjangoModelFactory): class Meta: model = MessageModel author = SubFactory(UserFactory) in_conversation = SubFactory(Conversation) content = LazyAttribute(lambda x: faker.text())
class GroupApplicationFactory(DjangoModelFactory): class Meta: model = GroupApplication questions = LazyAttribute( lambda application: application.group.application_questions) answers = LazyAttribute(lambda x: faker.text())
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 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 make_feedback(pickup, given_by): data = c.post('/api/feedback/', { 'comment': faker.text(), 'weight': 100.0, 'about': pickup, 'given_by': given_by, }).data print('created feedback: ', data) return data
def test_apply_for_group(self): self.client.force_login(user=self.applicant) answers = faker.text() # create application response = self.client.post( '/api/group-applications/', { 'group': self.group.id, 'answers': answers, }, ) self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data) data = response.data application_id = data['id'] del data['id'] conversation_id = data['conversation'] del data['conversation'] self.assertEqual( data, { 'questions': self.group.application_questions, 'answers': answers, 'user': self.applicant.id, 'group': self.group.id, 'status': 'pending', } ) # check conversation conversation_response = self.client.get('/api/conversations/{}/'.format(conversation_id)) self.assertEqual(conversation_response.status_code, status.HTTP_200_OK) for user_id in (self.applicant.id, self.member.id): self.assertIn(user_id, conversation_response.data['participants']) message_response = self.get_results('/api/messages/?conversation={}'.format(conversation_id)) self.assertEqual(len(message_response.data), 0) # list application application_list_response = self.client.get('/api/group-applications/') self.assertEqual(application_list_response.status_code, status.HTTP_200_OK) self.assertEqual(len(application_list_response.data), 1) self.assertEqual( application_list_response.data[0], { 'id': application_id, 'questions': self.group.application_questions, 'answers': answers, 'user': self.applicant.id, 'group': self.group.id, 'conversation': conversation_id, 'status': 'pending', } ) # check email notifications notification = mail.outbox[0] self.assertIn('wants to join', notification.subject) self.assertEqual(notification.to[0], self.member.email) self.assertEqual(len(mail.outbox), 1)
def test_cannot_apply_when_already_member(self): self.client.force_login(user=self.member) response = self.client.post( '/api/group-applications/', { 'group': self.group.id, 'answers': faker.text(), }, ) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_cannot_apply_with_unverified_account(self): user = UserFactory() self.client.force_login(user=user) response = self.client.post( '/api/group-applications/', { 'group': self.group.id, 'answers': faker.text(), }, ) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_cannot_apply_to_open_group(self): open_group = GroupFactory(members=[self.member], is_open=True) self.client.force_login(user=self.applicant) response = self.client.post( '/api/group-applications/', { 'group': open_group.id, 'answers': faker.text(), }, ) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def make_group(): data = c.post('/api/groups/', { 'name': 'Group ' + faker.city(), 'description': faker.text(), 'timezone': 'Europe/Berlin', 'address': faker.street_address(), 'latitude': faker.latitude(), 'longitude': faker.longitude() }).data print('created group: ', data['id'], data['name']) return data
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 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 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_cannot_have_two_pending_applications(self): GroupApplicationFactory(group=self.group, user=self.applicant) # create another application self.client.force_login(user=self.applicant) answers = faker.text() response = self.client.post( '/api/group-applications/', { 'group': self.group.id, 'answers': answers, }, ) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def setUp(self): self.group = GroupFactory() self.user = UserFactory() self.author = UserFactory() self.group.add_member(self.user) self.group.add_member(self.author) self.token = faker.uuid4() self.content = faker.text() self.conversation = self.group.conversation # add a push subscriber PushSubscription.objects.create(user=self.user, token=self.token, platform=PushSubscriptionPlatform.ANDROID)
def setUp(self): self.user = UserFactory() self.author = UserFactory() self.token = faker.uuid4() self.content = faker.text() # join a conversation self.conversation = ConversationFactory() self.conversation.join(self.user) self.conversation.join(self.author) # add a push subscriber PushSubscription.objects.create(user=self.user, token=self.token, platform=PushSubscriptionPlatform.ANDROID)
def get_or_create_application(): application = GroupApplication.objects.order_by('?').first() if application is None: new_user = VerifiedUserFactory() group = random_group() if group.application_questions == '': group.application_questions = faker.text() group.save() application = GroupApplicationFactory( group=group, user=new_user, ) return application
def make_group(): data = c.post( '/api/groups/', { 'name': 'Group ' + faker.city(), 'description': faker.text(), 'timezone': 'Europe/Berlin', 'address': faker.street_address(), 'latitude': faker.latitude(), 'longitude': faker.longitude() } ).data Group.objects.filter(id=data['id']).update(is_open=True) conversation = c.get('/api/groups/{}/conversation/'.format(data['id'])) data['conversation'] = conversation print('created group: ', data['id'], data['name']) return data
def setUp(self): self.user = UserFactory() self.author = UserFactory() self.token = faker.uuid4() self.content = faker.text() # join a conversation self.conversation = ConversationFactory(participants=[self.user, self.author]) # add a push subscriber self.subscription = PushSubscription.objects.create( user=self.user, token=self.token, platform=PushSubscriptionPlatform.ANDROID.value, )
def test_can_apply_again(self): GroupApplicationFactory( group=self.group, user=self.applicant, status=GroupApplicationStatus.WITHDRAWN.value, ) # create another application self.client.force_login(user=self.applicant) response = self.client.post( '/api/group-applications/', { 'group': self.group.id, 'answers': faker.text(), }, ) self.assertEqual(response.status_code, status.HTTP_201_CREATED)
def test_disable_notifications(self): self.client.force_login(user=self.member) response = self.client.delete( '/api/groups/{}/notification_types/{}/'.format( self.group.id, GroupNotificationType.NEW_APPLICATION, )) self.assertEqual(response.status_code, status.HTTP_200_OK) # create application self.client.force_login(user=self.applicant) answers = faker.text() self.client.post('/api/group-applications/', { 'group': self.group.id, 'answers': answers, }) # no emails should be received by member self.assertEqual(len(mail.outbox), 0)
def setUpClass(cls): super().setUpClass() cls.url = '/api/conversations/' # A chat with 2 participants cls.participant = UserFactory() cls.participant2 = UserFactory() cls.conversation = Conversation(participants=[cls.participant, cls.participant2]) cls.conversation_url = cls.url + str(cls.conversation.id) + '/' # not a participant cls.user = UserFactory() # another chat cls.conversation_data = {'topic': faker.name(), 'with_participants': [cls.participant2.id, ], 'message': faker.text()} cls.patch_data = {'topic': 'new topic'} cls.put_data = {'topic': 'new topic'}
class UserFactory(DjangoModelFactory): class Meta: model = get_user_model() strategy = CREATE_STRATEGY is_active = True is_staff = False display_name = LazyAttribute(lambda _: faker.name()) email = LazyAttribute(lambda _: faker.email()) description = LazyAttribute(lambda _: faker.text()) # Use display_name as password, as it is readable password = PostGeneration(lambda obj, *args, **kwargs: obj.set_password(obj.display_name)) @classmethod def _create(cls, model_class, *args, **kwargs): manager = cls._get_manager(model_class) user = manager.create_user(*args, **kwargs) return user
def make_message(conversation_id): data = c.post('/api/messages/', { 'content': faker.text(), 'conversation': conversation_id, }).data return data