def test_limit(self): c = Client() c.login(username='******',password='******') response = c.get('/api/hunt/') self.failUnlessEqual(response.status_code,200) obj = json.loads(response.content) self.assert_(obj['hunts']) hunts = obj['hunts'] self.assertEquals(len(hunts),1) # Follow the URLs and see what we get hunt = hunts[0] response = c.get(hunt['url']) self.failUnlessEqual(response.status_code,200) submit_url = hunt['submissions'] hunt_comments_url = hunt['comments'] f = testfiles.open_file('test1.jpg') photo1 = self._newSubmission(c,submit_url,f) photo2 = self._newSubmission(c,submit_url,f) photo3 = self._newSubmission(c,submit_url,f) # Get the submissions, there should be three submissions = self._getChildArray(c, submit_url, 'submissions') self.failUnlessEqual(len(submissions), 3) # Limit submissions = self._getChildArray(c, submit_url+"?limit=1", 'submissions') self.failUnlessEqual(len(submissions), 1) # Post a few comments (stagger them so we can predict their order) for t in ('one','two','three','four'): time.sleep(1) self._newComment(c,hunt_comments_url,t) comments = self._getChildArray(c, hunt_comments_url, 'comments') self.failUnlessEqual(len(comments), 4) # Limit comments = self._getChildArray(c, hunt_comments_url+"?limit=2", 'comments') self.failUnlessEqual(len(comments), 2) # Limit over how many there actually are comments = self._getChildArray(c, hunt_comments_url+"?limit=5", 'comments') self.failUnlessEqual(len(comments), 4) # Offset comments = self._getChildArray(c, hunt_comments_url+"?offset=1", 'comments') self.failUnlessEqual(len(comments), 3) self.failUnlessEqual(comments[0]['text'], 'three') self.failUnlessEqual(comments[1]['text'], 'two') self.failUnlessEqual(comments[2]['text'], 'one') # Offset off the end comments = self._getChildArray(c, hunt_comments_url+"?offset=5", 'comments') self.failUnlessEqual(len(comments), 0) # Offset + limit comments = self._getChildArray(c, hunt_comments_url+"?offset=2&limit=2", 'comments') self.failUnlessEqual(len(comments), 2) self.failUnlessEqual(comments[0]['text'], 'two') self.failUnlessEqual(comments[1]['text'], 'one')
def test_API(self): c = Client() response = c.get('/api/hunt/') self.failUnlessEqual(response.status_code,200) #self.failUnlessEqual(response['Content-Type'],'application/json') obj = json.loads(response.content) self.assert_(obj['hunts']) hunts = obj['hunts'] self.assertEquals(len(hunts),1) # Follow the URLs and see what we get hunt = hunts[0] response = c.get(hunt['url']) self.failUnlessEqual(response.status_code,200) response = c.get(hunt['api_url']) self.failUnlessEqual(response.status_code,200) submit_url = hunt['submissions'] ballot_url = hunt['ballot'] hunt_comments_url = hunt['comments'] # Ensure that the ballot url returns an error response = c.get(ballot_url) self.failUnlessEqual(response.status_code,400) response = c.get(submit_url) self.failUnlessEqual(response.status_code,200) #self.failUnlessEqual(response['Content-Type'],'application/json') # No submissions yet obj = json.loads(response.content) self.assertEquals(obj['submissions'],[]) from StringIO import StringIO # TODO: Test submssion -- authenticated and anonymous # photo, latitude, longitude, source_via # First test an invalid file response = c.post(submit_url, { 'photo': StringFile("testfile.txt","This is an invalid photo"), 'source_via': 'unit test' }) self.failUnlessEqual(response.status_code, 400) f = testfiles.open_file('test1.jpg') response = c.post(submit_url, { 'photo': f, 'via': 'unit test' }) self.failUnlessEqual(response.status_code, 201) #self.failUnlessEqual(response['Content-Type'],'application/json') photo1Url = response['Content-Location'] # Ensure that the ballot url returns an error response = c.get(ballot_url) self.failUnlessEqual(response.status_code,400) # Now log in and submit a photo c.login(username='******',password='******') f.seek(0) response = c.post(submit_url, { 'photo': f, 'via': 'unit test', 'valid_check':True }) self.failUnlessEqual(response.status_code, 201) #self.failUnlessEqual(response['Content-Type'],'application/json') # Ensure we have a proper user in the returned source obj = json.loads(response.content) self.assert_('source' in obj) self.assertEquals(obj['source']['name'], 'testdude') photo_comments_url = obj['comments'] # Get the submit url again and make sure it has two submissions response = c.get(submit_url) self.failUnlessEqual(response.status_code,200) #self.failUnlessEqual(response['Content-Type'],'application/json') obj = json.loads(response.content) self.assertEquals(len(obj['submissions']),2) # Test the submissions URL response = c.get(obj['submissions'][0]['api_url']) self.failUnlessEqual(response.status_code,200) # We should now have a valid ballot response = c.get(ballot_url) self.failUnlessEqual(response.status_code,200) #self.failUnlessEqual(response['Content-Type'],'application/json') obj = json.loads(response.content) self.assertEquals(len(obj['submissions']),2) response = c.post(ballot_url, {'url':obj['submissions'][0]['url'] }) # This should respond with a new ballot self.failUnlessEqual(response.status_code,200) #self.failUnlessEqual(response['Content-Type'],'application/json') # TODO: We also need some tests for: # Getting ballots and submitting votes for non-current hunts self.do_test_comment(c, hunt_comments_url, 'this is a hunt comment') self.do_test_comment(c, photo_comments_url, 'this is a photo comment')
def _newSubmission(self, c, hunt): f = testfiles.open_file('test1.jpg') response = c.post(hunt.get_submission_url(), { 'photo': f, 'via': 'unit test' }) self.failUnlessEqual(response.status_code, 201) return json.loads(response.content)