def test_adding_question_with_image(self): self._login() user = self.db.User.one(username='******') assert user # pretend this user goes to verify his email user_settings = self.db.UserSettings() user_settings.user = user._id user_settings.email_verified = user.email user_settings.save() maths = self.db.Genre() maths.name = u'Celebs' maths.approved = True maths.save() url = self.reverse_url('add_question') data = dict(text=u"H\ex3r mar dU? ", answer="Bra ", accept="Hyffsat", alternatives=" Bra \nOk\nFine\nIlla", genre="Life ", spell_correct='yes', comment="" ) # simulate clicking on the right submit button data['add_image'] = 1 response = self.client.post(url, data) self.assertEqual(response.code, 302) question, = self.db.Question.find() assert question.answer == u'Bra' new_image_url = self.reverse_url('new_question_image', question._id) self.assertEqual(response.headers['Location'], new_image_url) response = self.client.get(new_image_url) self.assertTrue('type="file"' in response.body) edit_url = self.reverse_url('edit_question', question._id) self.assertTrue(edit_url in response.body) here = os.path.dirname(__file__) image_data = open(os.path.join(here, 'image.png'), 'rb').read() assert image_data content_type, data = encode_multipart_formdata([('image', 'image.png')], [('image', 'image.png', image_data)]) response = self.client.post(new_image_url, data, headers={'Content-Type': content_type}) self.assertEqual(response.code, 302) question, = self.db.Question.find() submit_url = self.reverse_url('submit_question', question._id) self.assertEqual(response.headers['location'], submit_url) # don't submit yet but check that there's a thumbnail on the submit # confirmation page response = self.client.get(submit_url) img_tag = self._find_thumbnail_tag(response.body) self.assertTrue(img_tag) self.assertEqual(img_tag['alt'], question.text) response = self.client.get(img_tag['src']) from PIL import Image from cStringIO import StringIO img = Image.open(StringIO(response.body)) self.assertEqual(img.size, (75,18)) # known from fixture self.assertEqual(img_tag['width'], '75') self.assertEqual(img_tag['height'], '18') # go back and change image edit_url = self.reverse_url('edit_question', question._id) response = self.client.get(edit_url) img_tag = self._find_thumbnail_tag(response.body) self.assertTrue(img_tag) response = self.client.get(new_image_url) self.assertEqual(response.code, 200) img_tag = self._find_thumbnail_tag(response.body) self.assertTrue(img_tag) self.assertEqual(img_tag['alt'], question.text) image_data = open(os.path.join(here, 'screenshot.jpg'), 'rb').read() content_type, data = encode_multipart_formdata([('image', 'image.jpg')], [('image', 'image.jpg', image_data)]) response = self.client.post(new_image_url, data, headers={'Content-Type': content_type}) self.assertEqual(response.code, 302) self.assertEqual(response.headers['location'], submit_url) response = self.client.get(submit_url) img_tag = self._find_thumbnail_tag(response.body) self.assertTrue(img_tag) self.assertEqual(img_tag['alt'], question.text) response = self.client.get(img_tag['src']) from PIL import Image from cStringIO import StringIO img = Image.open(StringIO(response.body)) self.assertEqual(img.size, (300, 236)) # known from fixture self.assertEqual(img_tag['width'], '300') self.assertEqual(img_tag['height'], '236') # now submit it response = self.client.post(submit_url, {}) self.assertEqual(response.code, 302) submitted_url = self.reverse_url('submitted_question', question._id) self.assertEqual(submitted_url, response.headers['location']) # view that response = self.client.get(submitted_url) #print response.headers['location'] #self.assertEqual(response.code, 200) question, = self.db.Question.find() self.assertEqual(question.state, 'SUBMITTED') admin = TestClient(self) self._login('admin', email=settings.ADMIN_EMAILS[0], client=admin) response = admin.get(self.reverse_url('questions')) self.assertTrue('Submitted Questions (1)' in response.body) img_tag = self._find_thumbnail_tag(response.body) self.assertTrue(img_tag) response = self.client.get(img_tag['src']) img = Image.open(StringIO(response.body)) self.assertEqual(img.size, (20, 16)) # known from fixture self.assertEqual(img_tag['width'], '20') self.assertEqual(img_tag['height'], '16') response = admin.get(self.reverse_url('view_question', question._id)) img_tag = self._find_thumbnail_tag(response.body) self.assertTrue(img_tag) self.assertEqual(img_tag['alt'], question.text) response = self.client.get(img_tag['src']) img = Image.open(StringIO(response.body)) self.assertEqual(img.size, (300, 236)) # known from fixture self.assertEqual(img_tag['width'], '300') self.assertEqual(img_tag['height'], '236') accept_url = self.reverse_url('accept_question', question._id) response = admin.post(accept_url, {}) self.assertEqual(response.code, 302) question, = self.db.Question.find() self.assertEqual(question.state, 'ACCEPTED') bob = TestClient(self) self._login('bob', email='*****@*****.**', client=bob) random_review_url = self.reverse_url('review_random') response = bob.get(random_review_url) img_tag = self._find_thumbnail_tag(response.body) self.assertTrue(img_tag) self.assertEqual(img_tag['alt'], question.text) response = self.client.get(img_tag['src']) img = Image.open(StringIO(response.body)) self.assertEqual(img.size, (300, 236)) # known from fixture self.assertEqual(img_tag['width'], '300') self.assertEqual(img_tag['height'], '236') publish_url = self.reverse_url('publish_question', question._id) response = admin.post(publish_url, {}) self.assertEqual(response.code, 302) question, = self.db.Question.find() self.assertEqual(question.state, 'PUBLISHED') published_url = self.reverse_url('questions_published') response = self.client.get(published_url) img_tag = self._find_thumbnail_tag(response.body) self.assertTrue(img_tag) response = self.client.get(img_tag['src']) img = Image.open(StringIO(response.body)) self.assertEqual(img.size, (20, 16)) # known from fixture self.assertEqual(img_tag['width'], '20') self.assertEqual(img_tag['height'], '16')
def test_adding_question_with_image(self): self._login() user = self.db.User.one(username='******') assert user # pretend this user goes to verify his email user_settings = self.db.UserSettings() user_settings.user = user._id user_settings.email_verified = user.email user_settings.save() maths = self.db.Genre() maths.name = u'Celebs' maths.approved = True maths.save() url = self.reverse_url('add_question') data = dict(text=u"H\ex3r mar dU? ", answer="Bra ", accept="Hyffsat", alternatives=" Bra \nOk\nFine\nIlla", genre="Life ", spell_correct='yes', comment="") # simulate clicking on the right submit button data['add_image'] = 1 response = self.client.post(url, data) self.assertEqual(response.code, 302) question, = self.db.Question.find() assert question.answer == u'Bra' new_image_url = self.reverse_url('new_question_image', question._id) self.assertEqual(response.headers['Location'], new_image_url) response = self.client.get(new_image_url) self.assertTrue('type="file"' in response.body) edit_url = self.reverse_url('edit_question', question._id) self.assertTrue(edit_url in response.body) here = os.path.dirname(__file__) image_data = open(os.path.join(here, 'image.png'), 'rb').read() assert image_data content_type, data = encode_multipart_formdata( [('image', 'image.png')], [('image', 'image.png', image_data)]) response = self.client.post(new_image_url, data, headers={'Content-Type': content_type}) self.assertEqual(response.code, 302) question, = self.db.Question.find() submit_url = self.reverse_url('submit_question', question._id) self.assertEqual(response.headers['location'], submit_url) # don't submit yet but check that there's a thumbnail on the submit # confirmation page response = self.client.get(submit_url) img_tag = self._find_thumbnail_tag(response.body) self.assertTrue(img_tag) self.assertEqual(img_tag['alt'], question.text) response = self.client.get(img_tag['src']) from PIL import Image from cStringIO import StringIO img = Image.open(StringIO(response.body)) self.assertEqual(img.size, (75, 18)) # known from fixture self.assertEqual(img_tag['width'], '75') self.assertEqual(img_tag['height'], '18') # go back and change image edit_url = self.reverse_url('edit_question', question._id) response = self.client.get(edit_url) img_tag = self._find_thumbnail_tag(response.body) self.assertTrue(img_tag) response = self.client.get(new_image_url) self.assertEqual(response.code, 200) img_tag = self._find_thumbnail_tag(response.body) self.assertTrue(img_tag) self.assertEqual(img_tag['alt'], question.text) image_data = open(os.path.join(here, 'screenshot.jpg'), 'rb').read() content_type, data = encode_multipart_formdata( [('image', 'image.jpg')], [('image', 'image.jpg', image_data)]) response = self.client.post(new_image_url, data, headers={'Content-Type': content_type}) self.assertEqual(response.code, 302) self.assertEqual(response.headers['location'], submit_url) response = self.client.get(submit_url) img_tag = self._find_thumbnail_tag(response.body) self.assertTrue(img_tag) self.assertEqual(img_tag['alt'], question.text) response = self.client.get(img_tag['src']) from PIL import Image from cStringIO import StringIO img = Image.open(StringIO(response.body)) self.assertEqual(img.size, (300, 236)) # known from fixture self.assertEqual(img_tag['width'], '300') self.assertEqual(img_tag['height'], '236') # now submit it response = self.client.post(submit_url, {}) self.assertEqual(response.code, 302) submitted_url = self.reverse_url('submitted_question', question._id) self.assertEqual(submitted_url, response.headers['location']) # view that response = self.client.get(submitted_url) #print response.headers['location'] #self.assertEqual(response.code, 200) question, = self.db.Question.find() self.assertEqual(question.state, 'SUBMITTED') admin = TestClient(self) self._login('admin', email=settings.ADMIN_EMAILS[0], client=admin) response = admin.get(self.reverse_url('questions')) self.assertTrue('Submitted Questions (1)' in response.body) img_tag = self._find_thumbnail_tag(response.body) self.assertTrue(img_tag) response = self.client.get(img_tag['src']) img = Image.open(StringIO(response.body)) self.assertEqual(img.size, (20, 16)) # known from fixture self.assertEqual(img_tag['width'], '20') self.assertEqual(img_tag['height'], '16') response = admin.get(self.reverse_url('view_question', question._id)) img_tag = self._find_thumbnail_tag(response.body) self.assertTrue(img_tag) self.assertEqual(img_tag['alt'], question.text) response = self.client.get(img_tag['src']) img = Image.open(StringIO(response.body)) self.assertEqual(img.size, (300, 236)) # known from fixture self.assertEqual(img_tag['width'], '300') self.assertEqual(img_tag['height'], '236') accept_url = self.reverse_url('accept_question', question._id) response = admin.post(accept_url, {}) self.assertEqual(response.code, 302) question, = self.db.Question.find() self.assertEqual(question.state, 'ACCEPTED') bob = TestClient(self) self._login('bob', email='*****@*****.**', client=bob) random_review_url = self.reverse_url('review_random') response = bob.get(random_review_url) img_tag = self._find_thumbnail_tag(response.body) self.assertTrue(img_tag) self.assertEqual(img_tag['alt'], question.text) response = self.client.get(img_tag['src']) img = Image.open(StringIO(response.body)) self.assertEqual(img.size, (300, 236)) # known from fixture self.assertEqual(img_tag['width'], '300') self.assertEqual(img_tag['height'], '236') publish_url = self.reverse_url('publish_question', question._id) response = admin.post(publish_url, {}) self.assertEqual(response.code, 302) question, = self.db.Question.find() self.assertEqual(question.state, 'PUBLISHED') published_url = self.reverse_url('questions_published') response = self.client.get(published_url) img_tag = self._find_thumbnail_tag(response.body) self.assertTrue(img_tag) response = self.client.get(img_tag['src']) img = Image.open(StringIO(response.body)) self.assertEqual(img.size, (20, 16)) # known from fixture self.assertEqual(img_tag['width'], '20') self.assertEqual(img_tag['height'], '16')
def test_question_lifecycle(self): url = self.reverse_url('questions') response = self.client.get(url) self.assertEqual(response.code, 302) self.assertTrue('/login/' in response.headers['Location']) user = self._login() assert user # add a draft question add_url = self.reverse_url('add_question') response = self.client.get(url) self.assertEqual(response.code, 200) self.assertTrue(add_url in response.body) maths = self.db.Genre() maths.name = u'Maths' maths.save() assert not maths.approved question = self.db.Question() question.text = u'Who likes milk?' question.answer = u'cats' question.author = user question.state = DRAFT question.genre = maths question.save() question.add_date -= datetime.timedelta(minutes=1) edit_question_url = self.reverse_url('edit_question', question._id) response = self.client.get(url) self.assertEqual(response.code, 200) self.assertTrue(edit_question_url in response.body) self.assertTrue('draft questions (1)' in response.body.lower()) response = self.client.get(edit_question_url) self.assertEqual(response.code, 200) submit_question_url = self.reverse_url('submit_question', question._id) #assert not question.can_submit() response = self.client.post(submit_question_url, {}) self.assertEqual(response.code, 400) self.assertTrue(submit_question_url not in response.body) question.alternatives = [u'1', u'2', u'3', u'4'] question.save() #assert question.can_submit() response = self.client.get(edit_question_url) self.assertTrue(submit_question_url in response.body) response = self.client.get(submit_question_url) self.assertEqual(response.code, 200) admin = TestClient(self) self._login('admin', email=settings.ADMIN_EMAILS[0], client=admin) response = admin.get(submit_question_url) self.assertEqual(response.code, 200) dude = TestClient(self) self._login('greg', client=dude) response = dude.get(submit_question_url) self.assertEqual(response.code, 403) response = dude.post(submit_question_url, {}) self.assertEqual(response.code, 403) response = self.client.post(submit_question_url, {}) self.assertEqual(response.code, 302) submitted_url = self.reverse_url('submitted_question', question._id) # you can't post without first verifying your email address self.assertTrue('/settings/' in response.headers['location']) self.assertTrue('email=must' in response.headers['location']) # pretend this user goes to verify his email user_settings = self.db.UserSettings() user_settings.user = user._id user_settings.email_verified = user.email user_settings.save() response = self.client.post(submit_question_url, {}) self.assertEqual(response.code, 302) self.assertEqual(submitted_url, response.headers['location']) response = self.client.get(url) self.assertEqual(response.code, 200) self.assertTrue('submitted questions (1)' in response.body.lower()) self.assertTrue(edit_question_url not in response.body) view_question_url = self.reverse_url('view_question', question._id) self.assertTrue(view_question_url in response.body) # if you view it, observe that you're not allowed to review it response = self.client.get(view_question_url) self.assertEqual(response.code, 200) self.assertTrue(question.text in response.body) self.assertTrue('Submitted:' in response.body) response = dude.get(view_question_url) self.assertEqual(response.code, 403) # not accepted yet response = admin.get(view_question_url) self.assertEqual(response.code, 200) self.assertTrue('Admin actions' in response.body) # reject it first reject_question_url = self.reverse_url('reject_question', question._id) data = {'reject_comment': 'Too long'} response = self.client.post(reject_question_url, data) self.assertEqual(response.code, 403) # not an admin response = dude.post(reject_question_url, data) self.assertEqual(response.code, 403) # not an admin response = admin.post(reject_question_url, data) self.assertEqual(response.code, 302) question = self.db.Question.one({'_id':question._id}) assert question.state == REJECTED response = self.client.get(url) self.assertTrue('rejected questions (1)' in response.body.lower()) response = self.client.get(edit_question_url) self.assertEqual(response.code, 200) response = admin.get(edit_question_url) self.assertEqual(response.code, 200) data = dict(answer=question.answer, text="Short?", accept='', alternatives=[question.answer,'Dickens','Shake','Spere'], comment="\tchanged\n", genre="Geography", ) response = self.client.post(edit_question_url, data) self.assertEqual(response.code, 302) question = self.db.Question.one({'_id':question._id}) assert question.text == "Short?" assert self.db.Genre.one({'name':'Geography'}) # admin is allowed to do this too response = admin.post(edit_question_url, data) self.assertEqual(response.code, 302) question = self.db.Question.one({'_id':question._id}) assert question.state == DRAFT # this time, use the "Save and submit" instead data['submit_question'] = 'yes' response = self.client.post(edit_question_url, data) self.assertEqual(response.code, 302) question = self.db.Question.one({'_id':question._id}) assert question.state == SUBMITTED # as admin, accept it now accept_question_url = self.reverse_url('accept_question', question._id) response = admin.post(accept_question_url, {}) self.assertEqual(response.code, 302) self.assertEqual(response.headers['Location'], self.reverse_url('questions')) question = self.db.Question.one({'_id':question._id}) assert question.state == ACCEPTED response = self.client.get(url) self.assertTrue('submitted questions (0)' in response.body.lower()) # this other dude should be able to review the question response = dude.get(url) review_random_url = self.reverse_url('review_random') self.assertTrue(review_random_url in response.body) response = dude.get(review_random_url) self.assertEqual(response.code, 200) review_question_url = self.reverse_url('review_question', question._id) data = {'rating':'Good', 'difficulty': '-1', 'comment': 'No comment', 'verdict': 'VERIFIED', } response = self.client.post(review_question_url, data) self.assertEqual(response.code, 400) # can't review your own response = dude.post(review_question_url, data) self.assertEqual(response.code, 302) review = self.db.QuestionReview.one({'question.$id':question._id}) assert review self.assertEqual(review.comment, data['comment']) self.assertEqual(review.rating, 1) self.assertEqual(review.difficulty, int(data['difficulty'])) self.assertEqual(review.verdict, data['verdict']) assert review.user.username == 'greg' response = admin.get(view_question_url) publish_question_url = self.reverse_url('publish_question', question._id) self.assertEqual(response.code, 200) self.assertTrue(publish_question_url in response.body) response = self.client.post(publish_question_url, {}) self.assertEqual(response.code, 403) # not admin response = admin.post(publish_question_url, {}) self.assertEqual(response.code, 302) question = self.db.Question.one({'_id':question._id}) assert question.state == PUBLISHED maths = self.db.Genre.one(dict(name=maths.name)) self.assertTrue(not maths.approved) maths = self.db.Genre.one(dict(name=maths.name)) self.assertTrue(not maths.approved) geography = self.db.Genre.one({'name':'Geography'}) self.assertTrue(geography.approved)
def test_question_lifecycle(self): url = self.reverse_url('questions') response = self.client.get(url) self.assertEqual(response.code, 302) self.assertTrue('/login/' in response.headers['Location']) user = self._login() assert user # add a draft question add_url = self.reverse_url('add_question') response = self.client.get(url) self.assertEqual(response.code, 200) self.assertTrue(add_url in response.body) maths = self.db.Genre() maths.name = u'Maths' maths.save() assert not maths.approved question = self.db.Question() question.text = u'Who likes milk?' question.answer = u'cats' question.author = user question.state = DRAFT question.genre = maths question.save() question.add_date -= datetime.timedelta(minutes=1) edit_question_url = self.reverse_url('edit_question', question._id) response = self.client.get(url) self.assertEqual(response.code, 200) self.assertTrue(edit_question_url in response.body) self.assertTrue('draft questions (1)' in response.body.lower()) response = self.client.get(edit_question_url) self.assertEqual(response.code, 200) submit_question_url = self.reverse_url('submit_question', question._id) #assert not question.can_submit() response = self.client.post(submit_question_url, {}) self.assertEqual(response.code, 400) self.assertTrue(submit_question_url not in response.body) question.alternatives = [u'1', u'2', u'3', u'4'] question.save() #assert question.can_submit() response = self.client.get(edit_question_url) self.assertTrue(submit_question_url in response.body) response = self.client.get(submit_question_url) self.assertEqual(response.code, 200) admin = TestClient(self) self._login('admin', email=settings.ADMIN_EMAILS[0], client=admin) response = admin.get(submit_question_url) self.assertEqual(response.code, 200) dude = TestClient(self) self._login('greg', client=dude) response = dude.get(submit_question_url) self.assertEqual(response.code, 403) response = dude.post(submit_question_url, {}) self.assertEqual(response.code, 403) response = self.client.post(submit_question_url, {}) self.assertEqual(response.code, 302) submitted_url = self.reverse_url('submitted_question', question._id) # you can't post without first verifying your email address self.assertTrue('/settings/' in response.headers['location']) self.assertTrue('email=must' in response.headers['location']) # pretend this user goes to verify his email user_settings = self.db.UserSettings() user_settings.user = user._id user_settings.email_verified = user.email user_settings.save() response = self.client.post(submit_question_url, {}) self.assertEqual(response.code, 302) self.assertEqual(submitted_url, response.headers['location']) response = self.client.get(url) self.assertEqual(response.code, 200) self.assertTrue('submitted questions (1)' in response.body.lower()) self.assertTrue(edit_question_url not in response.body) view_question_url = self.reverse_url('view_question', question._id) self.assertTrue(view_question_url in response.body) # if you view it, observe that you're not allowed to review it response = self.client.get(view_question_url) self.assertEqual(response.code, 200) self.assertTrue(question.text in response.body) self.assertTrue('Submitted:' in response.body) response = dude.get(view_question_url) self.assertEqual(response.code, 403) # not accepted yet response = admin.get(view_question_url) self.assertEqual(response.code, 200) self.assertTrue('Admin actions' in response.body) # reject it first reject_question_url = self.reverse_url('reject_question', question._id) data = {'reject_comment': 'Too long'} response = self.client.post(reject_question_url, data) self.assertEqual(response.code, 403) # not an admin response = dude.post(reject_question_url, data) self.assertEqual(response.code, 403) # not an admin response = admin.post(reject_question_url, data) self.assertEqual(response.code, 302) question = self.db.Question.one({'_id': question._id}) assert question.state == REJECTED response = self.client.get(url) self.assertTrue('rejected questions (1)' in response.body.lower()) response = self.client.get(edit_question_url) self.assertEqual(response.code, 200) response = admin.get(edit_question_url) self.assertEqual(response.code, 200) data = dict( answer=question.answer, text="Short?", accept='', alternatives=[question.answer, 'Dickens', 'Shake', 'Spere'], comment="\tchanged\n", genre="Geography", ) response = self.client.post(edit_question_url, data) self.assertEqual(response.code, 302) question = self.db.Question.one({'_id': question._id}) assert question.text == "Short?" assert self.db.Genre.one({'name': 'Geography'}) # admin is allowed to do this too response = admin.post(edit_question_url, data) self.assertEqual(response.code, 302) question = self.db.Question.one({'_id': question._id}) assert question.state == DRAFT # this time, use the "Save and submit" instead data['submit_question'] = 'yes' response = self.client.post(edit_question_url, data) self.assertEqual(response.code, 302) question = self.db.Question.one({'_id': question._id}) assert question.state == SUBMITTED # as admin, accept it now accept_question_url = self.reverse_url('accept_question', question._id) response = admin.post(accept_question_url, {}) self.assertEqual(response.code, 302) self.assertEqual(response.headers['Location'], self.reverse_url('questions')) question = self.db.Question.one({'_id': question._id}) assert question.state == ACCEPTED response = self.client.get(url) self.assertTrue('submitted questions (0)' in response.body.lower()) # this other dude should be able to review the question response = dude.get(url) review_random_url = self.reverse_url('review_random') self.assertTrue(review_random_url in response.body) response = dude.get(review_random_url) self.assertEqual(response.code, 200) review_question_url = self.reverse_url('review_question', question._id) data = { 'rating': 'Good', 'difficulty': '-1', 'comment': 'No comment', 'verdict': 'VERIFIED', } response = self.client.post(review_question_url, data) self.assertEqual(response.code, 400) # can't review your own response = dude.post(review_question_url, data) self.assertEqual(response.code, 302) review = self.db.QuestionReview.one({'question.$id': question._id}) assert review self.assertEqual(review.comment, data['comment']) self.assertEqual(review.rating, 1) self.assertEqual(review.difficulty, int(data['difficulty'])) self.assertEqual(review.verdict, data['verdict']) assert review.user.username == 'greg' response = admin.get(view_question_url) publish_question_url = self.reverse_url('publish_question', question._id) self.assertEqual(response.code, 200) self.assertTrue(publish_question_url in response.body) response = self.client.post(publish_question_url, {}) self.assertEqual(response.code, 403) # not admin response = admin.post(publish_question_url, {}) self.assertEqual(response.code, 302) question = self.db.Question.one({'_id': question._id}) assert question.state == PUBLISHED maths = self.db.Genre.one(dict(name=maths.name)) self.assertTrue(not maths.approved) maths = self.db.Genre.one(dict(name=maths.name)) self.assertTrue(not maths.approved) geography = self.db.Genre.one({'name': 'Geography'}) self.assertTrue(geography.approved)