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() )
class ApplicationFactory(DjangoModelFactory): class Meta: model = Application questions = LazyAttribute( lambda application: application.group.application_questions) answers = LazyAttribute(lambda x: faker.text())
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_message(conversation_id): response = c.post('/api/messages/', { 'content': faker.text(), 'conversation': conversation_id, }) if response.status_code != 201: raise Exception('could not make message', conversation_id, response.data) return response.data
def test_cannot_apply_when_already_member(self): self.client.force_login(user=self.member) response = self.client.post( '/api/applications/', { 'group': self.group.id, 'answers': faker.text(), }, ) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def modify_place(place): response = c.patch( '/api/places/{}/'.format(place), { 'name': 'Place (edited) ' + faker.name(), 'description': faker.text(), }) if response.status_code != 200: raise Exception('could not modify place', place, response.data) print('modified place: ', place) return response.data
def modify_group(group): response = c.patch( '/api/groups/{}/'.format(group), { 'name': 'Group (edited) ' + faker.city(), 'description': faker.text(), }) if response.status_code != 200: raise Exception('could not modify group', group, response.data) print('modified group: ', group) return response.data
def apply_to_group(group): response = c.post('/api/applications/'.format(group), { 'answers': faker.text(), 'group': group, }) if response.status_code != 201: raise Exception('could not apply to group', group, response.data) print('applied to group {}'.format(group)) return response.data
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/applications/', { 'group': open_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/applications/', { 'group': self.group.id, 'answers': faker.text(), }, ) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_create_offer_without_image(self): self.client.force_login(user=self.user) data = { 'name': faker.name(), 'description': faker.text(), 'group': self.group.id, 'images': [], } response = self.client.post('/api/offers/', data=encode_data_with_images(data)) self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data) self.assertEqual(response.data['name'], data['name'])
def make_feedback(pickup, given_by): response = c.post( '/api/feedback/', { 'comment': faker.text(), 'weight': 100.0, 'about': pickup, 'given_by': given_by, }) if response.status_code != 201: raise Exception('could not make feedback', pickup, response.data) print('created feedback: ', response.data) return response.data
def test_cannot_have_two_pending_applications(self): ApplicationFactory(group=self.group, user=self.applicant) # create another application self.client.force_login(user=self.applicant) answers = faker.text() response = self.client.post( '/api/applications/', { 'group': self.group.id, 'answers': answers, }, ) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def get_or_create_application(): application = Application.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 = ApplicationFactory( group=group, user=new_user, ) return application
def setUp(self): self.user = UserFactory() self.member = UserFactory() self.group = GroupFactory(members=[self.member], is_open=True) self.group_with_password = GroupFactory(password='******', is_open=True) 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 setUp(self): self.user = UserFactory() self.member = UserFactory() self.group = GroupFactory(members=[self.member, UserFactory()], is_open=True) 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_create_offer(self): self.client.force_login(user=self.user) with open(image_path, 'rb') as image_file: data = { 'name': faker.name(), 'description': faker.text(), 'group': self.group.id, 'images': [{ 'position': 0, 'image': image_file }], } response = self.client.post('/api/offers/', data=encode_offer_data(data)) self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data) self.assertEqual(response.data['name'], data['name']) self.assertTrue('full_size' in response.data['images'][0]['image_urls'])
def test_create_offer(self, prepare_email): self.client.force_login(user=self.user) with open(image_path, 'rb') as image_file: data = { 'name': faker.name(), 'description': faker.text(), 'group': self.group.id, 'images': [{ 'position': 0, 'image': image_file }], } response = self.client.post('/api/offers/', data=encode_offer_data(data)) self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data) args, kwargs = prepare_email.call_args self.assertIsNotNone(kwargs['context']['offer_photo'])
def make_place(group): response = c.post( '/api/places/', { 'name': 'Place ' + faker.name(), 'description': faker.text(), 'group': group, 'address': faker.street_address(), 'latitude': faker.latitude(), 'longitude': faker.longitude(), 'status': 'active' }) if response.status_code != 201: raise Exception('could not make place', response.data) data = response.data print('created place: ', data['id'], data['name']) return data
def test_can_apply_again(self): ApplicationFactory( group=self.group, user=self.applicant, status=ApplicationStatus.WITHDRAWN.value, ) # create another application self.client.force_login(user=self.applicant) response = self.client.post( '/api/applications/', { 'group': self.group.id, 'answers': faker.text(), }, ) self.assertEqual(response.status_code, status.HTTP_201_CREATED)
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 make_group(): response = c.post( '/api/groups/', { 'name': 'Group ' + faker.city(), 'description': faker.text(), 'timezone': 'Europe/Berlin', 'address': faker.street_address(), 'latitude': faker.latitude(), 'longitude': faker.longitude() }) if response.status_code != 201: raise Exception('could not make group', response.data) data = response.data conversation = c.get('/api/groups/{}/conversation/'.format( data['id'])).data data['conversation'] = conversation print('created group: ', data['id'], data['name']) return data
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 self.subscription = PushSubscription.objects.create( user=self.user, token=self.token, platform=PushSubscriptionPlatform.ANDROID.value, )
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/applications/', { 'group': self.group.id, 'answers': answers, }) # no emails should be received by member self.assertEqual(len(mail.outbox), 0)
def make_group(country_code='DE'): lat, lng, city, country, timezone = faker.local_latlng( country_code=country_code) response = c.post( '/api/groups/', { 'name': 'Group ' + city, 'description': faker.text(), 'timezone': 'Europe/Berlin', 'address': faker.street_address() + ', ' + city, 'latitude': lat, 'longitude': lng }) if response.status_code != 201: raise Exception('could not make group', response.data) data = response.data conversation = c.get('/api/groups/{}/conversation/'.format( data['id'])).data data['conversation'] = conversation print('created group: ', data['id'], data['name']) return data
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 = Sequence(lambda n: str(n) + 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 test_apply_for_group(self): self.client.force_login(user=self.applicant) answers = faker.text() # create application response = self.client.post( '/api/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'] created_at = parse(data['created_at']) data['created_at'] = created_at self.assertEqual( data, { 'questions': self.group.application_questions, 'answers': answers, 'user': UserSerializer(self.applicant).data, 'group': self.group.id, 'status': 'pending', 'created_at': created_at, 'decided_by': None, 'decided_at': None, }, ) # get conversation conversation_response = self.client.get('/api/applications/{}/conversation/'.format(application_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']) conversation_id = conversation_response.data['id'] message_response = self.get_results('/api/messages/?conversation={}'.format(conversation_id)) self.assertEqual(len(message_response.data), 0) # list application application_list_response = self.get_results('/api/applications/') self.assertEqual(application_list_response.status_code, status.HTTP_200_OK) self.assertEqual(len(application_list_response.data), 1) data = application_list_response.data[0] data['created_at'] = parse(data['created_at']) self.assertEqual( data, { 'id': application_id, 'questions': self.group.application_questions, 'answers': answers, 'user': UserSerializer(self.applicant).data, 'group': self.group.id, 'status': 'pending', 'created_at': created_at, 'decided_by': None, 'decided_at': None, }, ) # 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)