def test_retrieve_silo_superuser(self): request = self.factory.get('/api/silo/') request.user = factories.User.build(is_superuser=True, is_staff=True) view = SiloViewSet.as_view({'get': 'retrieve'}) response = view(request, id=self.silos[0].id) self.assertEqual(response.status_code, 200) self.assertEqual(response.data['name'], self.silos[0].name)
def test_list_silo_superuser(self): request = self.factory.get('/api/silo/') request.user = factories.User.build(is_superuser=True, is_staff=True) view = SiloViewSet.as_view({'get': 'list'}) response = view(request) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 2)
def test_data_silo_owner(self): request = self.factory.get('/api/silo/{}/data'.format(self.silo.id)) request.user = self.tola_user.user view = SiloViewSet.as_view({'get': 'data'}) response = view(request, id=self.silo.id) self.assertEqual(response.status_code, 200) json_content = json.loads(response.content) self.assertEqual(json_content['recordsTotal'], 20) self.assertEqual(json_content['recordsFiltered'], 20)
def test_retrieve_silo_normal_user(self): request = self.factory.get('/api/silo/') request.user = self.tola_user.user view = SiloViewSet.as_view({'get': 'retrieve'}) response = view(request, id=self.silos[0].id) self.assertEqual(response.status_code, 404) silo = factories.Silo(name='My Silo', owner=self.tola_user.user) response = view(request, id=silo.id) self.assertEqual(response.status_code, 200) self.assertEqual(response.data['name'], silo.name)
def test_list_silo_shared(self): user = factories.User(first_name='Marge', last_name='Simpson') factories.Silo(owner=user, shared=[self.user]) request = self.factory.get('/api/silo/?user_uuid={}'.format( self.tola_user.tola_user_uuid)) request.user = self.tola_user.user view = SiloViewSet.as_view({'get': 'list'}) response = view(request) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1)
def test_list_silo_normal_user(self): request = self.factory.get('/api/silo/') request.user = self.tola_user.user view = SiloViewSet.as_view({'get': 'list'}) response = view(request) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 0) factories.Silo(owner=self.tola_user.user) response = view(request) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1)
def test_data_silo_query(self): query = '{"opn": "2015-11"}' request = self.factory.get('/api/silo/{}/data?query={}'.format( self.silo.id, query)) request.user = self.tola_user.user view = SiloViewSet.as_view({'get': 'data'}) response = view(request, id=self.silo.id) self.assertEqual(response.status_code, 200) json_content = json.loads(response.content) self.assertEqual(json_content['recordsTotal'], 3) self.assertEqual(json_content['recordsFiltered'], 3)
def test_data_silo_empty_table(self): read = factories.Read(read_name="test_empty", owner=self.tola_user.user) silo = factories.Silo(owner=self.tola_user.user, reads=[read]) request = self.factory.get('/api/silo/{}/data'.format(silo.id)) request.user = self.tola_user.user view = SiloViewSet.as_view({'get': 'data'}) response = view(request, id=silo.id) self.assertEqual(response.status_code, 200) json_content = json.loads(response.content) self.assertEqual(json_content['recordsTotal'], 0) self.assertEqual(json_content['recordsFiltered'], 0)
def test_data_silo_group(self): group = '{"_id": null,"total_cnt":{"$sum":"$cnt"}}' request = self.factory.get('/api/silo/{}/data?group={}'.format( self.silo.id, group)) request.user = self.tola_user.user view = SiloViewSet.as_view({'get': 'data'}) response = view(request, id=self.silo.id) self.assertEqual(response.status_code, 200) json_content = json.loads(response.content) self.assertEqual(json_content['recordsTotal'], 1) self.assertEqual(json_content['recordsFiltered'], 1) res = json_content['data'][0] self.assertEqual(res['total_cnt'], 74376)
def test_data_silo_superuser(self): another_user = factories.User(first_name='Homer', last_name='Simpson', is_superuser=True) factories.TolaUser(user=another_user) request = self.factory.get('/api/silo/{}/data'.format(self.silo.id)) request.user = another_user view = SiloViewSet.as_view({'get': 'data'}) response = view(request, id=self.silo.id) self.assertEqual(response.status_code, 200) json_content = json.loads(response.content) self.assertEqual(json_content['recordsTotal'], 20) self.assertEqual(json_content['recordsFiltered'], 20)
def test_save_data_customform_normaluser(self): user = factories.User(first_name='Homer', last_name='Simpson') sender_hikaya_user = factories.TolaUser(user=user) data = { 'silo_id': self.silo.id, 'data': { 'name': 'John Lennon', 'age': 40, 'city': 'Liverpool' }, 'submitted_by': sender_hikaya_user.hikaya_user_uuid.__str__(), } request = self.factory.post('api/customform/save_data', data=json.dumps(data), content_type='application/json') request.user = self.hikaya_user.user view = CustomFormViewSet.as_view({'post': 'save_data'}) response = view(request) self.assertEqual(response.status_code, 200) self.assertEqual(response.data['detail'], 'It was successfully saved.') self.assertEqual(self.silo.data_count, 1) request = self.factory.get('/api/silo/{}/data'.format(self.silo.id)) request.user = self.hikaya_user.user view = SiloViewSet.as_view({'get': 'data'}) response = view(request, id=self.silo.id) json_content = json.loads(response.content) data = json_content['data'][0] self.assertEqual(data['name'], 'John Lennon') self.assertEqual(data['age'], 40) self.assertEqual(data['city'], 'Liverpool') # check the submission date submission_date = datetime.now().strftime('%Y-%m-%d') self.assertIn('submission_date', data) self.assertEqual(data['submission_date'], submission_date) # the time can be different if the request takes a while self.assertIn('submission_time', data) self.assertTrue(data['submission_time']) # check the name of who sent the data self.assertIn('submitted_by', data) self.assertEqual(data['submitted_by'], sender_hikaya_user.name)
def test_data_silo_sort(self): query = '{"opn": "2015-11"}' request = self.factory.get('/api/silo/{}/data?query={}'.format( self.silo.id, query)) request.user = self.tola_user.user view = SiloViewSet.as_view({'get': 'data'}) response = view(request, id=self.silo.id) self.assertEqual(response.status_code, 200) json_content = json.loads(response.content) data = json_content['data'] last_rank = int(data[0]['rank']) for d in data[1:]: current_rank = int(d['rank']) self.assertTrue(last_rank < current_rank) last_rank = current_rank
def test_save_data_customform_superuser(self): self.hikaya_user.user.is_staff = True self.hikaya_user.user.is_superuser = True self.hikaya_user.user.save() data = { 'silo_id': self.silo.id, 'data': { 'name': 'John Lennon', 'age': 40, 'city': 'Liverpool' } } request = self.factory.post('api/customform/save_data', data=json.dumps(data), content_type='application/json') request.user = self.hikaya_user.user view = CustomFormViewSet.as_view({'post': 'save_data'}) response = view(request) self.assertEqual(response.status_code, 200) self.assertEqual(response.data['detail'], 'It was successfully saved.') self.assertEqual(self.silo.data_count, 1) request = self.factory.get('/api/silo/{}/data'.format(self.silo.id)) request.user = self.hikaya_user.user view = SiloViewSet.as_view({'get': 'data'}) response = view(request, id=self.silo.id) json_content = json.loads(response.content) data = json_content['data'][0] self.assertEqual(data['name'], 'John Lennon') self.assertEqual(data['age'], 40) self.assertEqual(data['city'], 'Liverpool') # check the submission date submission_date = datetime.now().strftime('%Y-%m-%d') self.assertIn('submission_date', data) self.assertEqual(data['submission_date'], submission_date) # the time can be different if the request takes a while self.assertIn('submission_time', data) self.assertTrue(data['submission_time']) # it shouldn't have a submitted_by because it wasn't provided self.assertNotIn('submitted_by', data)