def test_add_shared_user(self): # Make sure we are starting with the anticipated number of users. self.assertEqual(User.objects.count(), 1) # Assign an email to the existing user User.objects.update(email=self.existing_email, username=self.existing_username) query = DataQuery(template=True, default=True) query.save() self.assertEqual(query.shared_users.count(), 0) # Try add an existing user to shared users by username query.share_with_user(self.existing_username) self.assertEqual(query.shared_users.count(), 1) [query.share_with_user(e) for e in self.emails] # Looking up non existant users with usernames should not # create new users [query.share_with_user(u) for u in self.usernames] # Check that the user count increased for the email-based users # and no extra users were created when queried w/ username self.assertEqual(User.objects.count(), 4) # Check that the users are in the query's shared_users self.assertEqual(query.shared_users.count(), 4)
def test_put(self): # Add a query so we can try to update it later query = DataQuery(user=self.user, name='Query 1') query.save() response = self.client.get('/api/queries/1/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertTrue(response.content) # Attempt to update the name via a PUT request response = self.client.put('/api/queries/1/', data=u'{"name":"New Name"}', content_type='application/json') self.assertEqual(response.status_code, codes.ok) # Make sure our changes from the PUT request are persisted response = self.client.get('/api/queries/1/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertTrue(response.content) self.assertEqual(json.loads(response.content)['name'], 'New Name') # Make a PUT request with invalid JSON and make sure we get an # unprocessable status code back. response = self.client.put('/api/queries/1/', data=u'{"view_json":"[~][~]"}', content_type='application/json') self.assertEqual(response.status_code, codes.unprocessable_entity)
def test_no_create_on_share(self): # Make sure we are starting with the anticipated number of users. self.assertEqual(User.objects.count(), 1) # Assign an email to the existing user User.objects.all().update(email=self.existing_email) query = DataQuery(template=True, default=True) query.save() self.assertEqual(query.shared_users.count(), 0) # Test when both settings are False response = query.share_with_user(self.existing_email) self.assertEqual(response, False) with self.settings(AVOCADO_SHARE_BY_EMAIL=True): # Share with all the emails but, with create_user set to False, the # query should only be shared with the 1 existing user. [query.share_with_user(e, create_user=False) for e in self.emails] # Check that the user count increased for the email-based users self.assertEqual(User.objects.count(), 1) # Check that the users are in the query's shared_users self.assertEqual(query.shared_users.count(), 1)
def post(self, request, **kwargs): instance = self.get_object(request, **kwargs) if self._requestor_can_fork(request, instance): fork = DataQuery(name=instance.name, description=instance.description, view_json=instance.view_json, context_json=instance.context_json, parent=instance) if getattr(request, 'user', None): fork.user = request.user elif request.session.session_key: fork.session_key = request.session.session_key fork.save() request.session.modified = True posthook = functools.partial(query_posthook, request=request) data = serialize(fork, posthook=posthook, **templates.Query) return self.render(request, data, status=codes.created) data = { 'message': 'Cannot fork query', } return self.render(request, data, status=codes.unauthorized)
def test_get_session(self): query = DataQuery(user=self.user, name='Query', session=True) query.save() # All results for session query. response = self.client.get('/api/queries/session/results/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertTrue(response.content) # Single page of results for session query. response = self.client.get('/api/queries/session/results/3/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertTrue(response.content) # Page range of results for session query. response = self.client.get('/api/queries/session/results/1...5/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertTrue(response.content) query.session = False query.save() response = self.client.get('/api/queries/session/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.not_found)
def test_delete(self): query = DataQuery(user=self.user, name="TestQuery") query.save() session_query = DataQuery(user=self.user, name="SessionQuery", session=True) session_query.save() user1 = User(username='******', first_name='Shared', last_name='User', email='*****@*****.**') user1.save() query.shared_users.add(user1) user2 = User(username='******', first_name='Shared', last_name='User', email='') user2.save() query.shared_users.add(user2) user3 = User(username='******', first_name='Shared', last_name='User', email='*****@*****.**') user3.save() query.shared_users.add(user3) response = self.client.get('/api/queries/', HTTP_ACCEPT='application/json') self.assertEqual(len(json.loads(response.content)), 2) response = self.client.delete('/api/queries/1/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.no_content) # Since the delete handler send email asynchronously, wait for a while # while the mail goes through. time.sleep(5) # Make sure the mail was sent self.assertEqual(len(mail.outbox), 1) # Make sure the subject is correct self.assertEqual(mail.outbox[0].subject, "'TestQuery' has been deleted") # Make sure the recipient list is correct self.assertSequenceEqual( mail.outbox[0].to, ['*****@*****.**', '', '*****@*****.**']) response = self.client.get('/api/queries/', HTTP_ACCEPT='application/json') self.assertEqual(len(json.loads(response.content)), 1) # Make sure that we cannot delete the session query response = self.client.delete('/api/queries/2/') self.assertEqual(response.status_code, codes.bad_request) response = self.client.get('/api/queries/', HTTP_ACCEPT='application/json') self.assertEqual(len(json.loads(response.content)), 1)
def test_get(self): query = DataQuery(user=self.user) query.save() response = self.client.get('/api/queries/revisions/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertEqual(len(json.loads(response.content)), 1)
def test_session(self): query = DataQuery(session=True, user=self.user) query.save() response = self.client.get('/api/queries/session/stats/', HTTP_ACCEPT='application/json') data = json.loads(response.content) self.assertEqual(data['distinct_count'], 6) self.assertEqual(data['record_count'], 6)
def test_delete(self): query_name = u'ĘƞĵôƔ ťƕîš ǫųęŕƳ' query = DataQuery(user=self.user, name=query_name) query.save() session_query = DataQuery(user=self.user, name="SessionQuery", session=True) session_query.save() user1 = User(username='******', first_name='Shared', last_name='User', email='*****@*****.**') user1.save() query.shared_users.add(user1) user2 = User(username='******', first_name='Shared', last_name='User', email='') user2.save() query.shared_users.add(user2) user3 = User(username='******', first_name='Shared', last_name='User', email='*****@*****.**') user3.save() query.shared_users.add(user3) response = self.client.get('/api/queries/', HTTP_ACCEPT='application/json') self.assertEqual(len(json.loads(response.content)), 2) response = self.client.delete('/api/queries/{0}/'.format(query.pk), HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.no_content) # Since the delete handler send email asynchronously, wait for a while # while the mail goes through. time.sleep(5) # Make sure the mail was sent self.assertEqual(len(mail.outbox), 1) # Make sure the subject is correct self.assertEqual(mail.outbox[0].subject, u"'{0}' has been deleted".format(query_name)) # Make sure the recipient list is correct self.assertSequenceEqual( mail.outbox[0].to, ['*****@*****.**', '', '*****@*****.**']) response = self.client.get('/api/queries/', HTTP_ACCEPT='application/json') self.assertEqual(len(json.loads(response.content)), 1) # Make sure that we cannot delete the session query response = self.client.delete('/api/queries/{0}/' .format(session_query.pk)) self.assertEqual(response.status_code, codes.bad_request) response = self.client.get('/api/queries/', HTTP_ACCEPT='application/json') self.assertEqual(len(json.loads(response.content)), 1)
def test_duplicate_share(self): query = DataQuery(template=True, default=True) query.save() [query.share_with_user(e) for e in self.emails] share_count = query.shared_users.count() user_count = User.objects.count() # Make sure that requests to share with users that are already shared # with don't cause new user or shared_user entries. [query.share_with_user(e) for e in self.emails] self.assertEqual(share_count, query.shared_users.count()) self.assertEqual(user_count, User.objects.count())
def test_get_session(self): query = DataQuery(user=self.user, name='Query', session=True) query.save() response = self.client.get('/api/queries/session/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertTrue(response.content) query.session = False query.save() response = self.client.get('/api/queries/session/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.not_found)
def test_processor(self): query = DataQuery(session=True, user=self.user) query.save() response = self.client.get('/api/queries/{0}/stats/'.format(query.pk), HTTP_ACCEPT='application/json') data = json.loads(response.content) self.assertEqual(data['distinct_count'], 6) self.assertEqual(data['record_count'], 6) response = self.client.get('/api/queries/{0}/stats/?processor=manager' .format(query.pk), HTTP_ACCEPT='application/json') data = json.loads(response.content) self.assertEqual(data['distinct_count'], 1) self.assertEqual(data['record_count'], 1)
def test_processor(self): query = DataQuery(session=True, user=self.user) query.save() response = self.client.get('/api/queries/{0}/stats/'.format(query.pk), HTTP_ACCEPT='application/json') data = json.loads(response.content) self.assertEqual(data['distinct_count'], 6) self.assertEqual(data['record_count'], 6) response = self.client.get( '/api/queries/{0}/stats/?processor=manager'.format(query.pk), HTTP_ACCEPT='application/json') data = json.loads(response.content) self.assertEqual(data['distinct_count'], 1) self.assertEqual(data['record_count'], 1)
def test_only_owner(self): query = DataQuery(user=self.user) query.save() query2 = DataQuery() query2.save() # Ensure that there are 2 queries to start self.assertEqual(DataQuery.objects.count(), 2) response = self.client.get('/api/queries/', HTTP_ACCEPT='application/json') self.assertEqual(len(json.loads(response.content)), 1) query = json.loads(response.content)[0] self.assertTrue(query['is_owner']) self.assertTrue('shared_users' in query)
def test_owner_and_shared(self): # Create a query this user owns query = DataQuery(user=self.user) query.save() # Create a query owned by and shared with no one query2 = DataQuery() query2.save() # Create a query with no owner but shared with this user query3 = DataQuery() query3.save() query3.shared_users.add(self.user) query3.save() self.assertEqual(DataQuery.objects.count(), 3) # Retrieve the queries shared with and owned by this user, the count # should be 2 since this user owns one and is the sharee on another. response = self.client.get('/api/queries/', HTTP_ACCEPT='application/json') self.assertEqual(len(json.loads(response.content)), 2) # Verify that the order is descending based on accessed time. The 3rd # query was created most recently so it should be first in the list # over the 1st query. shared_queries = json.loads(response.content) self.assertEqual(shared_queries[0]['id'], query3.pk) self.assertEqual(shared_queries[1]['id'], query.pk) # Access the 1st query. This should make its accessed time update thus # making the 1st query the most recent of this users' shared queries. response = self.client.get('/api/queries/{0}/'.format(query.pk), HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) # Retrieve the queries shared with and owned by this user once again # to make sure the order has changed. response = self.client.get('/api/queries/', HTTP_ACCEPT='application/json') self.assertEqual(len(json.loads(response.content)), 2) # Since the 1st query was just accessed, it should now be the first # query in the result followed by the 3rd query. shared_queries = json.loads(response.content) self.assertEqual(shared_queries[0]['id'], query.pk) self.assertEqual(shared_queries[1]['id'], query3.pk) # If we logout and submit the request without a user, there should # be 0 shared queries returned. self.client.logout() response = self.client.get('/api/queries/', HTTP_ACCEPT='application/json') self.assertEqual(len(json.loads(response.content)), 0)
def test_add_shared_user(self): # Make sure we are starting with the anticipated number of users. self.assertEqual(User.objects.count(), 1) # Assign an email to the existing user User.objects.all().update(email=self.existing_email) query = DataQuery(template=True, default=True) query.save() self.assertEqual(query.shared_users.count(), 0) [query.share_with_user(e) for e in self.emails] # Check that the user count increased for the email-based users self.assertEqual(User.objects.count(), 4) # Check that the users are in the query's shared_users self.assertEqual(query.shared_users.count(), 4)
def test_get_session(self): # Make sure we have a session query. query = DataQuery(user=self.user, name='Query', session=True) query.save() # All results for session query. response = self.client.get('/api/async/queries/session/results/', HTTP_ACCEPT='application/json') self.assertEqual( response.status_code, HttpResponseRedirect.status_code) normal_job_id = response['Location'].split('/')[-2] # Single page of results for session query. response = self.client.get('/api/async/queries/session/results/3/', HTTP_ACCEPT='application/json') self.assertEqual( response.status_code, HttpResponseRedirect.status_code) paged_job_id = response['Location'].split('/')[-2] # Page range of results for session query. response = self.client.get('/api/async/queries/session/results/1...5/', HTTP_ACCEPT='application/json') self.assertEqual( response.status_code, HttpResponseRedirect.status_code) range_job_id = response['Location'].split('/')[-2] # The three requests above should have triggered 3 queued jobs. self.assertEqual(utils.get_job_count(), 3) for job_id in [normal_job_id, paged_job_id, range_job_id]: self.assert_job_status_equal( utils.get_job(job_id), 'queued') # Sleeping a couple seconds should leave plenty of time for the worker # to do its thing and finish up the three jobs from above. utils.run_jobs() time.sleep(3) # The three previous requests should now all be completed and their # items should match what we expect. for job_id in [normal_job_id, paged_job_id, range_job_id]: self.assert_job_status_equal( utils.get_job(job_id), 'finished') self.assert_job_result_equal(utils.get_job(job_id), [])
def test_session_owner(self): # No user for this one.. self.client.logout() # Access endpoint to initialize the anonymous session. This feels # like a hack, but there seems to be no other way to initialize the # session with a key self.client.get('/api/') # Fake the session key query = DataQuery(session_key=self.client.session.session_key) query.save() response = self.client.get('/api/queries/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, 200) self.assertEqual(len(json.loads(response.content)), 1) query = json.loads(response.content)[0] self.assertTrue(query['is_owner'])
def test_session_owner(self): # No user for this one.. self.client.logout() # Access endpoint to initialize the anonymous session. This feels # like a hack, but there seems to be no other way to initialize the # session with a key self.client.get('/api/') # Fake the session key query = DataQuery(session_key=self.client.session.session_key) query.save() response = self.client.get('/api/queries/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertEqual(len(json.loads(response.content)), 1) query = json.loads(response.content)[0] self.assertTrue(query['is_owner'])
def test_shared_users_count(self): u1 = User(username='******', email='*****@*****.**') u1.save() u2 = User(username='******', email='*****@*****.**') u2.save() query = DataQuery(user=self.user) query.save() query.shared_users.add(u1) query.shared_users.add(u2) query.save() response = self.client.get('/api/queries/', HTTP_ACCEPT='application/json') self.assertEqual(len(json.loads(response.content)), 1) content = json.loads(response.content)[0] self.assertEqual(len(content['shared_users']), 2) u3 = User(username='******', email='*****@*****.**') u3.save() u4 = User(username='******', email='*****@*****.**') u4.save() query.shared_users.remove(u1) query.shared_users.add(u3) query.shared_users.add(u4) query.save() response = self.client.get('/api/queries/', HTTP_ACCEPT='application/json') self.assertEqual(len(json.loads(response.content)), 1) content = json.loads(response.content)[0] self.assertEqual(len(content['shared_users']), 3)
def test_get(self): query = DataQuery(user=self.user) query.save() child_query = DataQuery(name='Child 1', parent=query) child_query.save() child_query = DataQuery(name='Child 2', parent=query) child_query.save() url = '/api/queries/{0}/'.format(query.pk) response = self.client.get(url, HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertTrue(response.content) self.assertLess(query.accessed, DataQuery.objects.get(pk=query.pk).accessed) # When we access a query it should contain a valid link to the forks # of that query. data = json.loads(response.content) response = self.client.get(data['_links']['forks']['href'], HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertTrue(response.content) self.assertEqual(len(json.loads(response.content)), 2) # Make sure we get a codes.not_found when accessing a query that # doesn't exist response = self.client.get('/api/queries/123456/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.not_found)
def test_clean(self): # Save default template query = DataQuery(template=True, default=True) query.save() # Save new template (not default) query2 = DataQuery(template=True) query2.save() # Try changing the second query to the default query2.default = True self.assertRaises(ValidationError, query2.save) query.save()
def test_get(self): query = DataQuery(name='Q1', public=True) query.save() query = DataQuery(name='Q2', public=True) query.save() query = DataQuery(name='Q3') query.save() self.assertEqual(DataQuery.objects.distinct().count(), 3) response = self.client.get('/api/queries/public/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertTrue(response.content) self.assertEqual(len(json.loads(response.content)), 2)
def test_get(self): query = DataQuery(user=self.user) query.save() child_query = DataQuery(name='Child 1', parent=query) child_query.save() child_query = DataQuery(name='Child 2', parent=query) child_query.save() url = '/api/queries/{0}/'.format(query.pk) response = self.client.get(url, HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertTrue(response.content) self.assertLess(query.accessed, DataQuery.objects.get(pk=query.pk).accessed) # When we access a query it should contain a valid link to the forks # of that query. self.assertTrue('forks' in response['Link-Template']) links = response['Link-Template'].split(',') for link in links: if 'forks' in link: # NOTE: This link template formatting would normally be done # on the client side but we do it manually once here to extract # and format the link to the forks. fields = link.split(';') href = fields[0].replace('<', '')\ .replace('>', '')\ .replace('{id}', str(query.pk))\ .strip() response = self.client.get(href, HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertTrue(response.content) self.assertEqual(len(json.loads(response.content)), 2) # Make sure we get a codes.not_found when accessing a query that # doesn't exist response = self.client.get('/api/queries/123456/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.not_found)
def test_get(self): query = DataQuery(user=self.user) query.save() child_query = DataQuery(name='Child 1', parent=query) child_query.save() child_query = DataQuery(name='Child 2', parent=query) child_query.save() url = '/api/queries/{0}/'.format(query.pk) response = self.client.get(url, HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertTrue(response.content) self.assertLess(query.accessed, DataQuery.objects.get(pk=query.pk).accessed) # When we access a query it should contain a valid link to the forks # of that query. self.assertTrue('forks' in response['Link-Template']) links = response['Link-Template'].split(',') for link in links: if 'forks' in link: # NOTE: This link template formatting would normally be done # on the client side but we do it manually once here to extract # and format the link to the forks. fields = link.split(';') href = fields[0].replace('<', '')\ .replace('>', '')\ .replace('{id}', str(query.pk))\ .strip() response = self.client.get( href, HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertTrue(response.content) self.assertEqual(len(json.loads(response.content)), 2) # Make sure we get a codes.not_found when accessing a query that # doesn't exist response = self.client.get('/api/queries/123456/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.not_found)
def test_shared_users_count(self): u1 = User(username='******', email='*****@*****.**') u1.save() u2 = User(username='******', email='*****@*****.**') u2.save() query = DataQuery(user=self.user) query.save() query.shared_users.add(u1) query.shared_users.add(u2) query.save() response = self.client.get('/api/queries/', HTTP_ACCEPT='application/json') self.assertEqual(len(json.loads(response.content)), 1) self.assertEqual( response['Link-Template'], ('<http://testserver/api/queries/{id}/>; rel="self", ' '<http://testserver/api/queries/{id}/stats/>; rel="stats", ' '<http://testserver/api/queries/{id}/forks/>; rel="forks", ' '<http://testserver/api/queries/{id}/results/>; rel="results"')) content = json.loads(response.content)[0] self.assertEqual(len(content['shared_users']), 2) u3 = User(username='******', email='*****@*****.**') u3.save() u4 = User(username='******', email='*****@*****.**') u4.save() query.shared_users.remove(u1) query.shared_users.add(u3) query.shared_users.add(u4) query.save() response = self.client.get('/api/queries/', HTTP_ACCEPT='application/json') self.assertEqual(len(json.loads(response.content)), 1) content = json.loads(response.content)[0] self.assertEqual(len(content['shared_users']), 3)
def test_shared_users_count(self): u1 = User(username='******', email='*****@*****.**') u1.save() u2 = User(username='******', email='*****@*****.**') u2.save() query = DataQuery(user=self.user) query.save() query.shared_users.add(u1) query.shared_users.add(u2) query.save() response = self.client.get('/api/queries/', HTTP_ACCEPT='application/json') self.assertEqual(len(json.loads(response.content)), 1) self.assertEqual(response['Link-Template'], ( '<http://testserver/api/queries/{id}/>; rel="self", ' '<http://testserver/api/queries/{id}/stats/>; rel="stats", ' '<http://testserver/api/queries/{id}/forks/>; rel="forks", ' '<http://testserver/api/queries/{id}/results/>; rel="results"' )) content = json.loads(response.content)[0] self.assertEqual(len(content['shared_users']), 2) u3 = User(username='******', email='*****@*****.**') u3.save() u4 = User(username='******', email='*****@*****.**') u4.save() query.shared_users.remove(u1) query.shared_users.add(u3) query.shared_users.add(u4) query.save() response = self.client.get('/api/queries/', HTTP_ACCEPT='application/json') self.assertEqual(len(json.loads(response.content)), 1) content = json.loads(response.content)[0] self.assertEqual(len(content['shared_users']), 3)
class QueryForksResourceTestCase(AuthenticatedBaseTestCase): def setUp(self): super(QueryForksResourceTestCase, self).setUp() self.public_query = DataQuery(name='Public Parent', public=True) self.public_query.save() self.user_query = DataQuery(name='Private User Parent', user=self.user) self.user_query.save() self.private_query = DataQuery(name='Private Parent') self.private_query.save() self.query = DataQuery(name='Shared Parent') self.query.save() self.query.shared_users.add(self.user) self.query.save() query = DataQuery(name='Child 1', parent=self.public_query) query.save() query = DataQuery(name='Child 2', parent=self.public_query) query.save() query = DataQuery(name='Child 3', parent=self.public_query) query.save() query = DataQuery(name='Child 4', parent=self.user_query) query.save() query = DataQuery(name='Child 5', parent=self.user_query) query.save() query = DataQuery(name='Child 6', parent=self.private_query) query.save() def test_post(self): query_count = DataQuery.objects.count() # We should be able to fork public queries url = '/api/queries/{0}/forks/'.format(self.public_query.pk) response = self.client.post(url, data='{}', content_type='application/json', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.created) self.assertEqual(DataQuery.objects.count(), query_count + 1) # ... and queries we own url = '/api/queries/{0}/forks/'.format(self.user_query.pk) response = self.client.post(url, data='{}', content_type='application/json') self.assertEqual(response.status_code, codes.created) self.assertEqual(DataQuery.objects.count(), query_count + 2) # ... and queries shared with us url = '/api/queries/{0}/forks/'.format(self.query.pk) response = self.client.post(url, data='{}', content_type='application/json') self.assertEqual(response.status_code, codes.created) self.assertEqual(DataQuery.objects.count(), query_count + 3) def test_post_unauthenticated(self): self.client.logout() url = '/api/queries/{0}/forks/'.format(self.user_query.pk) response = self.client.post(url, data='{}', content_type='application/json') self.assertEqual(response.status_code, codes.unauthorized) def test_post_unauthorized(self): url = '/api/queries/{0}/forks/'.format(self.private_query.pk) response = self.client.post(url, data='{}', content_type='application/json') self.assertEqual(response.status_code, codes.unauthorized) def test_post_invalid_pk(self): response = self.client.post('/api/queries/999999/forks/', data='{}', content_type='application/json') self.assertEqual(response.status_code, codes.not_found) def test_get_invalid_pk(self): response = self.client.get('/api/queries/999999/forks/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.not_found) def test_get_authenticated_owner(self): url = '/api/queries/{0}/forks/'.format(self.user_query.pk) response = self.client.get(url, HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertTrue(response.content) self.assertEqual(len(json.loads(response.content)), 2) def test_get_unauthenticated_owner(self): self.client.logout() url = '/api/queries/{0}/forks/'.format(self.user_query.pk) response = self.client.get(url, HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.unauthorized) def test_get_public(self): url = '/api/queries/{0}/forks/'.format(self.public_query.pk) response = self.client.get(url, HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertTrue(response.content) self.assertEqual(len(json.loads(response.content)), 3) def test_get_unauthorized(self): url = '/api/queries/{0}/forks/'.format(self.private_query.pk) response = self.client.get(url, HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.unauthorized)
class AsyncQueryResultsResourceTestCase(AuthenticatedBaseTestCase, JobTestCaseMixin): def setUp(self): super(AsyncQueryResultsResourceTestCase, self).setUp() utils.cancel_all_jobs() self.query = DataQuery(user=self.user) self.query.save() def test_get_bad_url(self): # This ID should not exist. response = self.client.get('api/async/queries/999/results/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.not_found) # We haven't created any DataQuery objects with session=True so there # should be no session query to get results for. response = self.client.get('/api/async/queries/session/results/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.not_found) def test_get(self): job = self.request_and_assert_job( '/api/async/queries/{0}/results/'.format(self.query.id)) # The job endpoint should tell us the job is queued since we don't # have any workers running yet. self.assert_job_status_equal(job, 'queued') # Since the job is queued and not yet processed, the result endpoint # should return a 404. response = self.client.get('/api/jobs/{0}/result/'.format(job.id), HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.not_found) # Let's start a worker, this should process the job which should cause # it to go to complete status and make its result available. utils.run_jobs() self.assert_job_status_equal(job, 'finished') self.assert_job_result_equal(job, []) def test_get_session(self): # Make sure we have a session query. query = DataQuery(user=self.user, name='Query', session=True) query.save() # All results for session query. response = self.client.get('/api/async/queries/session/results/', HTTP_ACCEPT='application/json') self.assertEqual( response.status_code, HttpResponseRedirect.status_code) normal_job_id = response['Location'].split('/')[-2] # Single page of results for session query. response = self.client.get('/api/async/queries/session/results/3/', HTTP_ACCEPT='application/json') self.assertEqual( response.status_code, HttpResponseRedirect.status_code) paged_job_id = response['Location'].split('/')[-2] # Page range of results for session query. response = self.client.get('/api/async/queries/session/results/1...5/', HTTP_ACCEPT='application/json') self.assertEqual( response.status_code, HttpResponseRedirect.status_code) range_job_id = response['Location'].split('/')[-2] # The three requests above should have triggered 3 queued jobs. self.assertEqual(utils.get_job_count(), 3) for job_id in [normal_job_id, paged_job_id, range_job_id]: self.assert_job_status_equal( utils.get_job(job_id), 'queued') # Sleeping a couple seconds should leave plenty of time for the worker # to do its thing and finish up the three jobs from above. utils.run_jobs() time.sleep(3) # The three previous requests should now all be completed and their # items should match what we expect. for job_id in [normal_job_id, paged_job_id, range_job_id]: self.assert_job_status_equal( utils.get_job(job_id), 'finished') self.assert_job_result_equal(utils.get_job(job_id), []) def test_page(self): job = self.request_and_assert_job( '/api/async/queries/{0}/results/3/'.format(self.query.id)) utils.run_jobs() self.assert_job_status_equal(job, 'finished') self.assert_job_result_equal(job, []) def test_page_range(self): job = self.request_and_assert_job( '/api/async/queries/{0}/results/3...50/'.format(self.query.id)) utils.run_jobs() self.assert_job_status_equal(job, 'finished') self.assert_job_result_equal(job, [])
class QueryForksResourceTestCase(AuthenticatedBaseTestCase): def setUp(self): super(QueryForksResourceTestCase, self).setUp() self.public_query = DataQuery(name='Public Parent', public=True) self.public_query.save() self.user_query = DataQuery(name='Private User Parent', user=self.user) self.user_query.save() self.private_query = DataQuery(name='Private Parent') self.private_query.save() self.query = DataQuery(name='Shared Parent') self.query.save() self.query.shared_users.add(self.user) self.query.save() query = DataQuery(name='Child 1', parent=self.public_query) query.save() query = DataQuery(name='Child 2', parent=self.public_query) query.save() query = DataQuery(name='Child 3', parent=self.public_query) query.save() query = DataQuery(name='Child 4', parent=self.user_query) query.save() query = DataQuery(name='Child 5', parent=self.user_query) query.save() query = DataQuery(name='Child 6', parent=self.private_query) query.save() def test_post(self): query_count = DataQuery.objects.count() # We should be able to fork public queries url = '/api/queries/{0}/forks/'.format(self.public_query.pk) response = self.client.post(url, data='{}', content_type='application/json', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.created) self.assertEqual(DataQuery.objects.count(), query_count + 1) # ... and queries we own url = '/api/queries/{0}/forks/'.format(self.user_query.pk) response = self.client.post(url, data='{}', content_type='application/json') self.assertEqual(response.status_code, codes.created) self.assertEqual(DataQuery.objects.count(), query_count + 2) # ... and queries shared with us url = '/api/queries/{0}/forks/'.format(self.query.pk) response = self.client.post(url, data='{}', content_type='application/json') self.assertEqual(response.status_code, codes.created) self.assertEqual(DataQuery.objects.count(), query_count + 3) def test_post_unauthenticated(self): self.client.logout() url = '/api/queries/{0}/forks/'.format(self.user_query.pk) response = self.client.post(url, data='{}', content_type='application/json') self.assertEqual(response.status_code, codes.unauthorized) def test_post_unauthorized(self): url = '/api/queries/{0}/forks/'.format(self.private_query.pk) response = self.client.post(url, data='{}', content_type='application/json') self.assertEqual(response.status_code, codes.unauthorized) def test_post_invalid_pk(self): response = self.client.post('/api/queries/999999/forks/', data='{}', content_type='application/json') self.assertEqual(response.status_code, codes.not_found) def test_get_invalid_pk(self): response = self.client.get('/api/queries/999999/forks/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.not_found) def test_get_authenticated_owner(self): url = '/api/queries/{0}/forks/'.format(self.user_query.pk) response = self.client.get(url, HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertTrue(response.content) self.assertEqual(len(json.loads(response.content)), 2) def test_get_unauthenticated_owner(self): self.client.logout() url = '/api/queries/{0}/forks/'.format(self.user_query.pk) response = self.client.get(url, HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.unauthorized) def test_get_public(self): url = '/api/queries/{0}/forks/'.format(self.public_query.pk) response = self.client.get(url, HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertTrue(response.content) self.assertEqual(len(json.loads(response.content)), 3) self.assertEqual(response['Link-Template'], ( '<http://testserver/api/queries/{id}/>; rel="self", ' '<http://testserver/api/queries/{parent_id}/>; rel="parent"' )) def test_get_unauthorized(self): url = '/api/queries/{0}/forks/'.format(self.private_query.pk) response = self.client.get(url, HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.unauthorized)
def setUp(self): super(QueryForksResourceTestCase, self).setUp() self.public_query = DataQuery(name='Public Parent', public=True) self.public_query.save() self.user_query = DataQuery(name='Private User Parent', user=self.user) self.user_query.save() self.private_query = DataQuery(name='Private Parent') self.private_query.save() self.query = DataQuery(name='Shared Parent') self.query.save() self.query.shared_users.add(self.user) self.query.save() query = DataQuery(name='Child 1', parent=self.public_query) query.save() query = DataQuery(name='Child 2', parent=self.public_query) query.save() query = DataQuery(name='Child 3', parent=self.public_query) query.save() query = DataQuery(name='Child 4', parent=self.user_query) query.save() query = DataQuery(name='Child 5', parent=self.user_query) query.save() query = DataQuery(name='Child 6', parent=self.private_query) query.save()
class QueryResultsResourceTestCase(AuthenticatedBaseTestCase): def setUp(self): super(QueryResultsResourceTestCase, self).setUp() self.query = DataQuery(user=self.user) self.query.save() def test_get(self): response = self.client.get('/api/queries/{0}/results/'.format( self.query.id), HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) # Let's just make sure that we didn't break anything regarding making # sure the query exists. response = self.client.get('api/queries/999/results/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.not_found) def test_get_session(self): query = DataQuery(user=self.user, name='Query', session=True) query.save() # All results for session query. response = self.client.get('/api/queries/session/results/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertTrue(response.content) # Single page of results for session query. response = self.client.get('/api/queries/session/results/3/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertTrue(response.content) # Page range of results for session query. response = self.client.get('/api/queries/session/results/1...5/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertTrue(response.content) query.session = False query.save() response = self.client.get('/api/queries/session/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.not_found) def test_page(self): # Page numbers must be greater than or equal to 1. response = self.client.get('/api/queries/{0}/results/0/'.format( self.query.id), HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.not_found) # Pages greater than 0 should be fine. response = self.client.get('/api/queries/{0}/results/3/'.format( self.query.id), HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) def test_page_range(self): # When using a page range, a 0 page should still be invalid. response = self.client.get('/api/queries/{0}/results/0...3/'.format( self.query.id), HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.not_found) # Stop pages less than start pages aren't a valid range. response = self.client.get('/api/queries/{0}/results/3...1/'.format( self.query.id), HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.not_found) # Stop pages >= start pages should be valid. response = self.client.get('/api/queries/{0}/results/3...50/'.format( self.query.id), HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) def test_delete(self): # There is nothing to delete here so we should be OK. response = self.client.delete('/api/queries/{0}/results/'.format( self.query.id), HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertEqual(json.loads(response.content), {'canceled': None}) # This should start an isolated query. response = self.client.get('/api/queries/{0}/results/'.format( self.query.id), HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) # The above GET request should have created an isolated query. So, we # get a value indicating that we have canceled it here. response = self.client.delete('/api/queries/{0}/results/'.format( self.query.id), HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertEqual(json.loads(response.content), {'canceled': True})
class QueryResultsResourceTestCase(AuthenticatedBaseTestCase): def setUp(self): super(QueryResultsResourceTestCase, self).setUp() self.query = DataQuery(user=self.user) self.query.save() def test_get(self): response = self.client.get( '/api/queries/{0}/results/'.format(self.query.id), HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) # Let's just make sure that we didn't break anything regarding making # sure the query exists. response = self.client.get('api/queries/999/results/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.not_found) def test_get_session(self): query = DataQuery(user=self.user, name='Query', session=True) query.save() # All results for session query. response = self.client.get('/api/queries/session/results/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertTrue(response.content) # Single page of results for session query. response = self.client.get('/api/queries/session/results/3/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertTrue(response.content) # Page range of results for session query. response = self.client.get('/api/queries/session/results/1...5/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertTrue(response.content) query.session = False query.save() response = self.client.get('/api/queries/session/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.not_found) def test_page(self): # Page numbers must be greater than or equal to 1. response = self.client.get( '/api/queries/{0}/results/0/'.format(self.query.id), HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.not_found) # Pages greater than 0 should be fine. response = self.client.get( '/api/queries/{0}/results/3/'.format(self.query.id), HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) def test_page_range(self): # When using a page range, a 0 page should still be invalid. response = self.client.get( '/api/queries/{0}/results/0...3/'.format(self.query.id), HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.not_found) # Stop pages less than start pages aren't a valid range. response = self.client.get( '/api/queries/{0}/results/3...1/'.format(self.query.id), HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.not_found) # Stop pages >= start pages should be valid. response = self.client.get( '/api/queries/{0}/results/3...50/'.format(self.query.id), HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) def test_delete(self): # There is nothing to delete here so we should be OK. response = self.client.delete( '/api/queries/{0}/results/'.format(self.query.id), HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertEqual( json.loads(response.content), {'canceled': None} ) # This should start an isolated query. response = self.client.get( '/api/queries/{0}/results/'.format(self.query.id), HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) # The above GET request should have created an isolated query. So, we # get a value indicating that we have canceled it here. response = self.client.delete( '/api/queries/{0}/results/'.format(self.query.id), HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertEqual( json.loads(response.content), {'canceled': True} )