def test_start_time_filter(self): """ Test that you can filter by start_time """ user = create_admin_user() view = KaznetTaskOccurrenceViewSet.as_view({'get': 'list'}) # make some poccurrences that start at 7 mommy.make('main.TaskOccurrence', _quantity=5, start_time='07:00') # make some occurrences that happen after 9:00 mommy.make('main.TaskOccurrence', _quantity=6, start_time='09:15') # test that we can get occurrences before or after a certain time request2 = self.factory.get('/occurrences', {'start_time__gte': '09:15'}) force_authenticate(request2, user=user) response2 = view(request=request2) self.assertEqual(response2.status_code, 200) self.assertEqual(len(response2.data['results']), 6) request3 = self.factory.get('/occurrences', {'start_time__lt': '09:15'}) force_authenticate(request3, user=user) response3 = view(request=request3) self.assertEqual(response3.status_code, 200) self.assertEqual(len(response3.data['results']), 5)
def test_submission_time(self): """ This method tests that all submissions passed through the serializer don't have microseconds in their submission_time fields and all submissions not passed through serializer have microseconds in their submission_time fields """ now = timezone.now() task = mommy.make('main.Task') bounty = mommy.make('main.Bounty') location = mommy.make('main.Location') user = mommy.make('auth.User') mommy.make('main.Submission', submission_time=now, id=20, user=user, task=task, bounty=bounty, location=location) view = KaznetSubmissionsViewSet.as_view({'get': 'list'}) user = create_admin_user() request = self.factory.get('/submissions', {'id': 20}) force_authenticate(request, user=user) response = view(request=request) self.assertEqual(response.status_code, 200) # ensure all the other fields have the same data submission = Submission.objects.filter(id=20).first() self.assertEqual(response.data['results'][0]['id'], submission.id) self.assertEqual( response.data['results'][0]['modified'], submission.modified.astimezone(pytz.timezone( 'Africa/Nairobi')).strftime("%Y-%m-%dT%H:%M:%S.%f%z")[:-2] + ":00") self.assertEqual( response.data['results'][0]['created'], submission.created.astimezone(pytz.timezone( 'Africa/Nairobi')).strftime("%Y-%m-%dT%H:%M:%S.%f%z")[:-2] + ":00") self.assertEqual(response.data['results'][0]['task']['id'], str(submission.task.id)) self.assertEqual(response.data['results'][0]['bounty']['id'], str(submission.bounty.id)) self.assertEqual(response.data['results'][0]['location']['id'], str(submission.location.id)) self.assertEqual(response.data['results'][0]['user']['id'], str(submission.user.id)) # ensure that the submission_timefield is exacly the same except # for microseconds self.assertEqual( response.data['results'][0]['submission_time'], submission.submission_time.astimezone( pytz.timezone('Africa/Nairobi')).strftime( "%Y-%m-%dT%H:%M:%S%z")[:-2] + ":00") self.assertFalse('.' in response.data['results'][0]['submission_time'])
def test_task_filter(self): """ Test Task Filter """ user = create_admin_user() task = mommy.make('main.Task') # make a bunch of bounties mommy.make('main.Bounty', _quantity=7) # make one bounty using the task bounty = mommy.make('main.Bounty', task=task) # check that we have 8 bounties # pylint: disable=no-member self.assertEqual(Bounty.objects.all().count(), 8) view = BountyViewSet.as_view({'get': 'list'}) # test that we get bounty for our task request = self.factory.get('/bounty', {'task_id': task.id}) force_authenticate(request, user=user) response = view(request=request) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data['results']), 1) self.assertEqual(response.data['results'][0]['id'], bounty.id)
def test_valid_sorting(self): """ Test that you can sort by valid """ user = create_admin_user() # make a bunch of submissions mommy.make('main.Submission', valid=False, _quantity=7) # make one submission where valid is True submission = mommy.make('main.Submission', valid=True) # check that we have 8 submissions # pylint: disable=no-member self.assertEqual(Submission.objects.all().count(), 8) view = KaznetSubmissionsViewSet.as_view({'get': 'list'}) # test sorting by valid request = self.factory.get('/submissions', {'ordering': 'valid'}) force_authenticate(request, user=user) response = view(request=request) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data['results']), 8) self.assertEqual(response.data['results'][0]['id'], Submission.objects.order_by('valid').first().id) self.assertEqual(len(response.data['results']), 8) self.assertEqual(response.data['results'][-1]['id'], submission.id)
def test_task_filter(self): """ Test that you can filter by task """ user = create_admin_user() task = mommy.make('main.Task') # make a bunch of occurrences mommy.make('main.TaskOccurrence', _quantity=7) # make one occurrence using the task occurrence = mommy.make('main.TaskOccurrence', task=task) # check that we have 8 occurrences # pylint: disable=no-member self.assertEqual(TaskOccurrence.objects.all().count(), 8) view = KaznetTaskOccurrenceViewSet.as_view({'get': 'list'}) # test that we get occurrences for our task request = self.factory.get('/occurrences', {'task': task.id}) force_authenticate(request, user=user) response = view(request=request) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data['results']), 1) self.assertEqual(response.data['results'][0]['id'], occurrence.id)
def test_list_ordering(self): """ test that you can get and order a list of userprofiles """ user = create_admin_user() # ADMIN & EXPERT & First name == 'Ona' ben = mommy.make('auth.User', first_name='ben') mosh = mommy.make('auth.User', first_name='mosh') kyle = mommy.make('auth.User', first_name='kyle') mommy.make('main.Submission', user=ben, _quantity=7) mommy.make('main.Submission', user=kyle, _quantity=9) mommy.make('main.Submission', user=mosh, _quantity=17) view = UserProfileViewSet.as_view({'get': 'list'}) # sort by submission count request1 = self.factory.get('/userprofiles', {'ordering': '-submission_count'}) force_authenticate(request=request1, user=user) response1 = view(request=request1) self.assertEqual(response1.status_code, 200) self.assertEqual('mosh', response1.data['results'][0]['first_name']) self.assertEqual('kyle', response1.data['results'][1]['first_name']) self.assertEqual('ben', response1.data['results'][2]['first_name']) # sort by first_name request1 = self.factory.get('/userprofiles', {'ordering': 'user__first_name'}) force_authenticate(request=request1, user=user) response1 = view(request=request1) self.assertEqual(response1.status_code, 200) self.assertEqual('ben', response1.data['results'][0]['first_name']) self.assertEqual('kyle', response1.data['results'][1]['first_name']) self.assertEqual('mosh', response1.data['results'][2]['first_name'])
def test_submission_filter(self): """ Test Submission_id filter """ user = create_admin_user() task = mommy.make('main.Task') mommy.make('main.Bounty', _quantity=7) bounty = mommy.make('main.Bounty', task=task) # Make a submission for the bounty submission = mommy.make('main.Submission', bounty=bounty) # check that we have 8 bounties # pylint: disable=no-member self.assertEqual(Bounty.objects.all().count(), 8) view = BountyViewSet.as_view({'get': 'list'}) # test that we get bounty for our task request = self.factory.get('/bounty', {'submission': submission.id}) force_authenticate(request, user=user) response = view(request=request) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data['results']), 1) self.assertEqual(response.data['results'][0]['id'], bounty.id)
def test_update(self): """ Test that you can update userprofiles """ user_data = self._create() user = create_admin_user() with requests_mock.Mocker() as mocked: mocked.patch( urljoin(settings.ONA_BASE_URL, f'api/v1/profiles/{user_data["ona_username"]}'), json=self.ona_json, status_code=200, ) mocked.put(urljoin(settings.ONA_BASE_URL, f'api/v1/orgs/{settings.ONA_ORG_NAME}/members'), status_code=200) data = { 'first_name': 'Peter', 'phone_number': '+254722111111', 'role': UserProfile.CONTRIBUTOR } view = UserProfileViewSet.as_view({'patch': 'partial_update'}) request = self.factory.patch(f'/userprofiles/{user_data["id"]}', data=data) force_authenticate(request, user=user) response = view(request=request, pk=user_data['id']) self.assertEqual(response.status_code, 200) self.assertEqual('Peter', response.data['first_name']) self.assertEqual('+254722111111', response.data['phone_number']) self.assertEqual(UserProfile.CONTRIBUTOR, response.data['role'])
def test_bounty_amount_sorting(self): """ Test that we can sort by bounty_amount """ user = create_admin_user() bounty1 = mommy.make('main.Bounty', amount=400) bounty2 = mommy.make('main.Bounty', amount=50) bounty3 = mommy.make('main.Bounty', amount=250) # make a bunch of submissions mommy.make('main.Submission', bounty=bounty3, _quantity=7) submission = mommy.make('main.Submission', bounty=bounty1) submission1 = mommy.make('main.Submission', bounty=bounty2) # check that we have 8 submissions # pylint: disable=no-member self.assertEqual(Submission.objects.all().count(), 9) view = KaznetSubmissionsViewSet.as_view({'get': 'list'}) # test sorting in descending order by bounty_amount request = self.factory.get('/submissions', {'ordering': '-bounty__amount'}) force_authenticate(request, user=user) response = view(request=request) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data['results']), 9) self.assertEqual(response.data['results'][0]['id'], submission.id) self.assertEqual(response.data['results'][-1]['id'], submission1.id)
def test_submission_modified_sorting(self): """ Test that we can Order By Modified """ user = create_admin_user() submission1 = mommy.make('main.Submission') # make a bunch of Submissions mommy.make('main.Submission', _quantity=7) submission2 = mommy.make('main.Submission') view = KaznetSubmissionsViewSet.as_view({'get': 'list'}) # test sorting in descending order by modified request = self.factory.get('/submissions', {'ordering': '-modified'}) force_authenticate(request, user=user) response = view(request=request) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data['results']), 9) self.assertEqual( parse(response.data['results'][-1]['modified']).astimezone( pytz.utc), submission1.modified) self.assertEqual(response.data['results'][-1]['id'], submission1.id) self.assertEqual( parse(response.data['results'][0]['modified']).astimezone( pytz.utc), submission2.modified) self.assertEqual(response.data['results'][0]['id'], submission2.id) self.assertTrue(response.data['results'][-1]['modified'] < response.data['results'][0]['modified'])
def test_list_searching(self): """ test that you can get and search a list of userprofiles """ user = create_admin_user() # ADMIN & EXPERT ben = mommy.make('auth.User', first_name='ben') ben.userprofile.national_id = 123456 ben.userprofile.save() alice = mommy.make('auth.User', first_name='alice') alice.userprofile.national_id = 89768 alice.userprofile.save() joe = mommy.make('auth.User', first_name='joe') joe.userprofile.national_id = 1337 joe.userprofile.save() view = UserProfileViewSet.as_view({'get': 'list'}) # search by national_id request = self.factory.get('/userprofiles', {'search': 1337}) force_authenticate(request=request, user=user) response = view(request=request) self.assertEqual(response.status_code, 200) self.assertEqual(1, len(response.data['results'])) self.assertEqual('joe', response.data['results'][0]['first_name']) # search by last name request2 = self.factory.get('/userprofiles', {'search': 'alice'}) force_authenticate(request=request2, user=user) response2 = view(request=request2) self.assertEqual(response2.status_code, 200) self.assertEqual(1, len(response2.data['results'])) self.assertEqual('alice', response2.data['results'][0]['first_name'])
def test_csv_export_modified_filter(self): """ Test CSV export modified filter """ user = create_admin_user() # lets get one of the submissions we had created earlier the_one_submission = Submission.objects.get(pk=999) # make 15 other random submissions mommy.make('main.Submission', _quantity=15) view = SubmissionExportViewSet.as_view({'get': 'list'}) # filter to only get submissions modified before the_one_submission request = self.factory.get( '/exports/submissions', { 'format': 'csv', 'modified__lte': str(the_one_submission.modified) }) force_authenticate(request, user=user) response = view(request=request) self.assertEqual(response.status_code, 200) self.assertTrue(response.streaming) received = BytesIO(b''.join(response.streaming_content)).getvalue() # we have filtered out the 15 new submissions, check our data self.assertEqual(self.expected, received)
def test_modified_filter(self): """ Test that you can filter by modified """ user = create_admin_user() task = mommy.make('main.Task') the_one_submission = mommy.make('main.Submission', task=task) # make a bunch of submissions mommy.make('main.Submission', _quantity=10, task=task) view = KaznetSubmissionsViewSet.as_view({'get': 'list'}) # test that we get the submission with our unique modified datetime request = self.factory.get( '/submissions', {'modified': str(the_one_submission.modified)}) force_authenticate(request, user=user) response = view(request=request) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data['results']), 1) self.assertEqual(response.data['results'][0]['id'], the_one_submission.id) # test we can get submissions modified after a certain time request = self.factory.get( '/submissions', {'modified__gt': str(the_one_submission.modified)}) force_authenticate(request, user=user) response = view(request=request) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data['results']), 10)
def test_location_filter(self): """ Test that you can filter by location """ user = create_admin_user() location = mommy.make('main.Location') # make a bunch of submissions mommy.make('main.Submission', _quantity=7) # make one submission using the location submission = mommy.make('main.Submission', location=location) # check that we have 8 submissions # pylint: disable=no-member self.assertEqual(Submission.objects.all().count(), 8) view = KaznetSubmissionsViewSet.as_view({'get': 'list'}) # test that we get submissions for our location request = self.factory.get('/submissions', {'location': location.id}) force_authenticate(request, user=user) response = view(request=request) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data['results']), 1) self.assertEqual(response.data['results'][0]['id'], submission.id)
def test_status_filter(self): """ Test that you can filter by status """ user = create_admin_user() # make a bunch of submissions mommy.make('main.Submission', status=Submission.PENDING, _quantity=7) # make one submission where status is APPROVED submission = mommy.make('main.Submission', status=Submission.APPROVED) # check that we have 8 submissions # pylint: disable=no-member self.assertEqual(Submission.objects.all().count(), 8) view = KaznetSubmissionsViewSet.as_view({'get': 'list'}) # test that we get approved submissions request = self.factory.get('/submissions', {'status': Submission.APPROVED}) force_authenticate(request, user=user) response = view(request=request) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data['results']), 1) self.assertEqual(response.data['results'][0]['id'], submission.id) # test that we get pending submissions request = self.factory.get('/submissions', {'status': Submission.PENDING}) force_authenticate(request, user=user) response = view(request=request) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data['results']), 7)
def test_national_id(self): """ Ensure the national ID field is updated correctly """ user_data = self._create() user = create_admin_user() with requests_mock.Mocker() as mocked: mocked.patch( urljoin(settings.ONA_BASE_URL, f'api/v1/profiles/{user_data["ona_username"]}'), json=self.ona_json, status_code=200, ) mocked.put(urljoin(settings.ONA_BASE_URL, f'api/v1/orgs/{settings.ONA_ORG_NAME}/members'), status_code=200) data = { 'first_name': 'Bob', 'national_id': '987654321', } view = UserProfileViewSet.as_view({'patch': 'partial_update'}) request = self.factory.patch(f'/userprofiles/{user_data["id"]}', data=data) force_authenticate(request, user=user) response = view(request=request, pk=user_data['id']) self.assertEqual(response.status_code, 200) self.assertEqual("987654321", response.data['national_id'])
def test_create_with_bad_data(self): """ Test that we get appropriate errors when trying to create an object with bad data """ bob_user = create_admin_user() data_missing_radius = { 'name': 'Nairobi', 'geopoint': '30,10', } view = KaznetLocationViewSet.as_view({'post': 'create'}) request = self.factory.post('/locations', data_missing_radius) # Need authenticated user force_authenticate(request, user=bob_user) response = view(request=request) self.assertEqual(response.status_code, 400) self.assertEqual(RADIUS_MISSING, str(response.data[0]['detail'])) data_missing_geopoint = { 'name': 'Montreal', 'radius': 45.678 } view1 = KaznetLocationViewSet.as_view({'post': 'create'}) request1 = self.factory.post('/locations', data_missing_geopoint) # Need authenticated user force_authenticate(request1, user=bob_user) response1 = view1(request=request1) self.assertEqual(response1.status_code, 400) self.assertEqual(GEOPOINT_MISSING, str(response1.data[0]['detail'])) path = os.path.join( BASE_DIR, 'fixtures', 'test_shapefile.zip') with open(path, 'r+b') as shapefile: data_shapefile = dict( name='Arusha', radius=56.6789, geopoint='30,10', shapefile=shapefile ) view2 = KaznetLocationViewSet.as_view({'post': 'create'}) request2 = self.factory.post('/locations', data_shapefile) # Need authenticated user force_authenticate(request2, user=bob_user) response2 = view2(request=request2) self.assertEqual(response2.status_code, 400) self.assertEqual(GEODETAILS_ONLY, str(response2.data[0]['detail']))
def test_retrieve_client(self): """ Test GET /client/[pk] return a client matching pk. """ user = create_admin_user() client = mommy.make('main.Client', name='Bob') view = ClientViewSet.as_view({'get': 'retrieve'}) request = self.factory.get('/client/{id}'.format(id=client.id)) force_authenticate(request, user=user) response = view(request=request, pk=client.id) self.assertEqual(response.status_code, 200) self.assertTrue(response.data['name'], client.name)
def test_retrieve_location_type(self): """ Test GET /locationtypes/[pk] return a project matching pk. """ user = create_admin_user() locationtype_data = self._create_location_type() view = KaznetLocationTypeViewSet.as_view({'get': 'retrieve'}) request = self.factory.get( '/locationtypes/{id}'.format(id=locationtype_data['id'])) force_authenticate(request, user=user) response = view(request=request, pk=locationtype_data['id']) self.assertEqual(response.status_code, 200) self.assertDictEqual(response.data, locationtype_data)
def test_list_submissions(self): """ Test we are able to list submissions """ user = create_admin_user() mommy.make('main.Submission', _quantity=7) view = KaznetSubmissionsViewSet.as_view({'get': 'list'}) request = self.factory.get('/submissions') force_authenticate(request, user=user) response = view(request=request) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data['results']), 7)
def test_list_location_type(self): """ Test GET /locationtypes listing of locationtypes """ user = create_admin_user() locationtype_data = self._create_location_type() view = KaznetLocationTypeViewSet.as_view({'get': 'list'}) request = self.factory.get('/locationtypes') force_authenticate(request, user=user) response = view(request=request) self.assertEqual(response.status_code, 200) self.assertDictEqual(response.data['results'].pop(), locationtype_data)
def test_list_occurrences(self): """ Test GET /occurrence listing of occurrences for specific forms. """ user = create_admin_user() occurrence_data = self._create_occurrence() view = KaznetTaskOccurrenceViewSet.as_view({'get': 'list'}) request = self.factory.get('/occurrence') force_authenticate(request, user=user) response = view(request=request) self.assertEqual(response.status_code, 200) self.assertDictEqual(response.data['results'].pop(), occurrence_data)
def test_retrieve_occurrence(self): """ Test GET /occurrence/[pk] return a occurrence matching pk. """ user = create_admin_user() occurrence_data = self._create_occurrence() view = KaznetTaskOccurrenceViewSet.as_view({'get': 'retrieve'}) request = self.factory.get( '/occurrence/{id}'.format(id=occurrence_data['id'])) force_authenticate(request, user=user) response = view(request=request, pk=occurrence_data['id']) self.assertEqual(response.status_code, 200) self.assertDictEqual(response.data, occurrence_data)
def test_location_sorting(self): """ Test that sorting works """ user = create_admin_user() project1 = mommy.make('main.Location', name='Nairobi') project2 = mommy.make('main.Location', name='Arusha') view = KaznetLocationViewSet.as_view({'get': 'list'}) # order by name descending request = self.factory.get('/locations', {'ordering': '-name'}) force_authenticate(request, user=user) response = view(request=request) self.assertEqual( response.data['results'][0]['name'], project1.name) self.assertEqual(response.data['results'][0]['id'], project1.id) self.assertEqual( response.data['results'][-1]['name'], project2.name) self.assertEqual(response.data['results'][-1]['id'], project2.id) # order by created ascending request = self.factory.get('/locations', {'ordering': 'created'}) force_authenticate(request, user=user) response = view(request=request) self.assertEqual( response.data['results'][0]['created'], project1.created.astimezone( pytz.timezone('Africa/Nairobi')).isoformat()) self.assertEqual(response.data['results'][0]['id'], project1.id) self.assertEqual( response.data['results'][-1]['created'], project2.created.astimezone( pytz.timezone('Africa/Nairobi')).isoformat()) self.assertEqual(response.data['results'][-1]['id'], project2.id) # order by modified descending request = self.factory.get('/locations', {'ordering': '-modified'}) force_authenticate(request, user=user) response = view(request=request) self.assertEqual( response.data['results'][-1]['modified'], project1.modified.astimezone( pytz.timezone('Africa/Nairobi')).isoformat()) self.assertEqual(response.data['results'][-1]['id'], project1.id) self.assertEqual( response.data['results'][0]['modified'], project2.modified.astimezone( pytz.timezone('Africa/Nairobi')).isoformat()) self.assertEqual(response.data['results'][0]['id'], project2.id)
def test_userprofile_filter(self): """ Test able to filter by userprofile """ user = create_admin_user() random = mommy.make('auth.User') dave = mommy.make('auth.User', username='******', first_name='Dave') daves_profile = dave.userprofile # make a bunch of submissions mommy.make('main.Submission', _quantity=7) # make one submission using the user dave submission = mommy.make('main.Submission', user=dave) # check that we have 8 submissions # pylint: disable=no-member self.assertEqual(Submission.objects.all().count(), 8) view = KaznetSubmissionsViewSet.as_view({'get': 'list'}) # test that we get submissions for dave request = self.factory.get('/submissions', {'userprofile': daves_profile.id}) force_authenticate(request, user=user) response = view(request=request) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data['results']), 1) self.assertEqual(response.data['results'][0]['id'], submission.id) self.assertEqual(int(response.data['results'][0]['user']['id']), daves_profile.user.id) # test dave can filter for his own submissions request2 = self.factory.get('/submissions', {'userprofile': daves_profile.id}) force_authenticate(request2, user=dave) response2 = view(request=request2) self.assertEqual(response2.status_code, 200) self.assertEqual(len(response2.data['results']), 1) self.assertEqual(response2.data['results'][0]['id'], submission.id) self.assertEqual(int(response2.data['results'][0]['user']['id']), daves_profile.user.id) # test random users can't filter for daves submissions request3 = self.factory.get('/submissions', {'userprofile': daves_profile.id}) force_authenticate(request3, user=random) response3 = view(request=request3) self.assertEqual(response3.status_code, 403) self.assertEqual(str(response3.data[0]['detail']), 'You shall not pass.')
def test_list_clients(self): """ Test GET /client returns all clients """ user = create_admin_user() mommy.make('main.Client', name='Solair') mommy.make('main.CLient', name='Generic', _quantity=7) view = ClientViewSet.as_view({'get': 'list'}) request = self.factory.get('/client') force_authenticate(request, user=user) response = view(request=request) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data['results']), 8)
def test_list_xform(self): """ Test that GET /forms returns a list of all xforms """ user = create_admin_user() mommy.make('ona.XForm', _quantity=4) view = XFormViewSet.as_view({'get': 'list'}) request = self.factory.get('/forms') force_authenticate(request, user=user) response = view(request=request) self.assertEqual(response.status_code, 200) self.assertEqual(4, len(response.data['results']))
def _create(self): """ Helper to create userprofiles with viewset """ with requests_mock.Mocker() as mocked: mocked.post(urljoin(settings.ONA_BASE_URL, 'api/v1/profiles'), status_code=201, json=self.ona_json) mocked.post(urljoin( settings.ONA_BASE_URL, f'api/v1/teams/{settings.ONA_MEMBERS_TEAM_ID}/members'), status_code=201) mocked.put(urljoin(settings.ONA_BASE_URL, f'api/v1/orgs/{settings.ONA_ORG_NAME}/members'), status_code=200) user = create_admin_user() data = { 'first_name': 'Bob', 'last_name': 'Doe', 'email': '*****@*****.**', 'password': '******', 'gender': UserProfile.MALE, 'role': UserProfile.ADMIN, 'expertise': UserProfile.EXPERT, 'national_id': '123456789', 'payment_number': '+254722222222', 'phone_number': '+254722222222', 'ona_pk': 1337, 'ona_username': '******' } view = UserProfileViewSet.as_view({'post': 'create'}) request = self.factory.post('/userprofiles', data) # need to authenticate force_authenticate(request, user=user) response = view(request=request) # assert that we get the right status_code and data back self.assertEqual(response.status_code, 201) # We remove password field since password is write-only data.pop('password') self.assertDictContainsSubset(data, response.data) return response.data
def test_retrieve_bounty(self): """ Test GET /bounty/[pk] return a bounty matching pk. """ user = create_admin_user() bounty_data = self._create_bounty() view = BountyViewSet.as_view({'get': 'retrieve'}) request = self.factory.get( '/bounty/{id}'.format(id=bounty_data['id'])) force_authenticate(request, user=user) response = view(request=request, pk=bounty_data['id']) self.assertEqual(response.status_code, 200) self.assertDictEqual(response.data, bounty_data)
def test_list_bounty(self): """ Test GET /bounty listing of bounty for specific forms. """ user = create_admin_user() bounty_data = self._create_bounty() view = BountyViewSet.as_view({'get': 'list'}) request = self.factory.get('/bounty') force_authenticate(request, user=user) response = view(request=request) self.assertEqual(response.status_code, 200) self.assertDictEqual(response.data['results'].pop(), bounty_data)