Skip to content

Commit

Permalink
Merge pull request #15 from hogum/ch-test-questions-163108185
Browse files Browse the repository at this point in the history
Ch test questions 163108185
  • Loading branch information
mugoh committed Jan 10, 2019
2 parents 98f07c3 + 935c251 commit 5a11e03
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 4 deletions.
8 changes: 6 additions & 2 deletions app/v1/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ def admin_required(f):
@wraps(f)
def wrapper(*args, **kwargs):

if not get_jwt_identity():
# We never get here in real sense, consumed by missing Auth header
# Uncomment when testing manually
#
# Verify Logged in
"""if not get_jwt_identity():
return {
"Status": 403,
"Error": "Please log in, okay?"
Expand All @@ -26,7 +30,7 @@ def wrapper(*args, **kwargs):
"Status": 400,
"Error": "Identity unknown"
}

"""
if not UserModel.get_by_name(get_jwt_identity()).isAdmin:
return {
"Status": 403,
Expand Down
3 changes: 1 addition & 2 deletions app/v1/views/questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,11 @@ def post(self):

# Verify meetup to be added to question record

"""if not MeetUpModel.get_by_id(args['meetup']):
if not MeetUpModel.get_by_id(args['meetup']):
return {
"Status": 404,
"Message": "Meetup id non-existent. Maybe create it?"
}, 404
"""

new_questn = QuestionModel(**args)

Expand Down
146 changes: 146 additions & 0 deletions tests/v1/question_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
from .base_test import BaseTestCase
from app.v1.models.questions import QuestionModel
import json


class TestQuestions(BaseTestCase):

def test_create_new_question(self):
self.new_question = json.dumps(dict(
title="One Question",
body="This looks lika a body",
meetup=1))

response = self.client.post('api/v1/questions',
data=self.new_question,
content_type='application/json',
headers=self.auth_header)

# Verify meetup refrenced exists

response_confirm_meetup = self.client.get(
'api/v1/meetups/1',
content_type='application/json',
headers=self.auth_header)

self.assertEqual(response_confirm_meetup.status_code, 200,
msg="Fails to fetch meetup for question")

self.assertEqual(response.status_code, 201,
msg="Fails to create a new question")

def test_create_existing_question(self):
new_question = json.dumps(dict(
title="One Question Dup",
body="This looks like a body"))

self.client.post('api/v1/questions',
data=new_question,
content_type='application/json',
headers=self.auth_header)

response = self.client.post('api/v1/questions',
data=new_question,
content_type='application/json',
headers=self.auth_header)
self.assertEqual(response.status_code, 409,
msg="Fails to not create\
a question with same data twice")

def test_create_new_question_that_references_nonexistent_meetup(self):
self.new_question = json.dumps(dict(
title="One Question",
body="This looks lika a body",
meetup=400))

response = self.client.post('api/v1/questions',
data=self.new_question,
content_type='application/json',
headers=self.auth_header)
response_confirm_meetup = self.client.get(
'api/v1/meetups/400',
content_type='application/json',
headers=self.auth_header)

self.assertEqual(response_confirm_meetup.status_code, 404,
msg="Fails to not create question\
for a non-existemt meetup")

self.assertEqual(response.status_code, 404,
msg="Fails to not create question\
for a non-existemt meetup")

def test_get_all_questions(self):

response = self.client.get('api/v1/questions',
content_type='application/json',
headers=self.auth_header)
self.assertEqual(response.status_code, 200,
msg="Fails to get all questions")

def test_get_single_question(self):

response = self.client.get('api/v1/questions/1',
content_type='application/json',
headers=self.auth_header)
self.assertEqual(response.status_code, 200,
msg="Fails to fetch individual question")

def test_get_non_existent_question(self):

response = self.client.get('api/v1/questions/400',
content_type='application/json',
headers=self.auth_header)
self.assertEqual(response.status_code, 404,
msg="Fails to return error\
on fetching missing question")

def test_down_vote_question(self):
res = self.client.patch('api/v1/questions/1/downvote',
content_type='application/json',
headers=self.auth_header)

expected_votes = res.get_json().get('Data')[0].get('votes')

self.assertEqual(expected_votes, -1,
msg="Fails to downvote a question")

def test_up_vote_question(self):
res = self.client.patch('api/v1/questions/1/upvote',
content_type='application/json',
headers=self.auth_header)

expected_votes = res.get_json().get('Data')[0].get('votes')

self.assertEqual(expected_votes, 0,
msg="Fails to upvote a question")

def test_vote_non_existent_question(self):
res = self.client.patch('api/v1/questions/500/upvote',
content_type='application/json',
headers=self.auth_header)

self.assertEqual(res.status_code, 404,
msg="Fails. Votes a missing question")

def test_vote_question_with_invalid_string(self):
res = self.client.patch('api/v1/questions/1/invalid',
content_type='application/json',
headers=self.auth_header)

self.assertEqual(res.status_code, 400,
msg="Fails to validate vote request")

def test_change_vote_on_question_instance_directly(self):
new_question = dict(
title="One Question Again",
body="This looks like a semi-body",
meetup=400
)

res = QuestionModel(**new_question)

with self.assertRaises(AttributeError) as ctx:
res.votes = 300
self.assertTrue('Oops! You are not allowed to do that' in
str(ctx.exception))

0 comments on commit 5a11e03

Please sign in to comment.