def test_unauthorized_access(self): url = "/questions/" data = {} # get request = self.factory.get(url) view = QuestionAPI.as_view() response = view(request) self.assertEqual( response.status_code, status.HTTP_401_UNAUTHORIZED, "Response status code %s was not 401" % response.status_code, ) # post data = { "title": "Why does the sun go down every night?", "content": "Why does the sun go down every night? I don't understand!", "tags": ["tagFirst", "tagSecond"], } request = self.factory.post(url, data) response = view(request) self.assertEqual( response.status_code, status.HTTP_401_UNAUTHORIZED, "Response status code %s was not 401" % response.status_code, )
def test_duplicates(self): flush_questions() url = "/questions/" data = { "title": "Why does the sun go down every night?", "content": "Why does the sun go down every night? I don't understand!", "tags": ["tagFirst", "tagSecond"], } request = self.factory.post(url, data) request.user = User.objects.get(username="******") view = QuestionAPI.as_view() response = view(request) self.assertEqual( response.status_code, status.HTTP_201_CREATED, "Response status code %s was not 201" % response.status_code ) request = self.factory.post(url, data) request.user = User.objects.get(username="******") response = view(request) # Creating same question twice self.assertEqual( response.status_code, status.HTTP_400_BAD_REQUEST, "Response status code %s was not 400" % response.status_code, )
def create_question(self): data = { "head":{ "title": "Why does the sun go down every night?" }, "content": "Why does the sun go down every night? I don't understand!", "tags": ["tagFirst", "tagSecond"] } response = self.create_message("/questions/", data, QuestionAPI.as_view()) return response.data
def test_scandinavian(self): flush_questions() # Try scandinavian url = "/questions/" data = { "title": u"Tämä on suomenkielinen kysymys, vai onko?", "content": u"Miksi ä, ö ja å -kirjamet eivät toimi", "tags": ["tagFirst", "tagSecond"], } request = self.factory.post(url, data) request.user = User.objects.get(username="******") view = QuestionAPI.as_view() response = view(request) self.assertEqual( response.status_code, status.HTTP_201_CREATED, "Response status code %s was not 201" % response.status_code ) self.validate_question(response.data, data, request.user)
def test_authorized_access(self): flush_questions() # get empty url = "/questions/" request = self.factory.get(url) request.user = User.objects.get(username="******") view = QuestionAPI.as_view() response = view(request) self.assertEqual( response.status_code, status.HTTP_404_NOT_FOUND, "Response status code %s was not 404" % response.status_code, ) data = { "title": "Why does the sun go down every night?", "content": "Why does the sun go down every night? I don't understand!", "tags": ["tagFirst", "tagSecond"], } request = self.factory.post(url, data) request.user = User.objects.get(username="******") response = view(request) self.assertEqual( response.status_code, status.HTTP_201_CREATED, "Response status code %s was not 201" % response.status_code ) # test question attributes self.validate_question(response.data, data, request.user) # test populated get url = "/questions/" request = self.factory.get(url) request.user = User.objects.get(username="******") response = view(request) self.assertEqual( response.status_code, status.HTTP_200_OK, "Response status code %s was not 200" % response.status_code )
def test_edit(self): flush_questions() # Create initial question url = "/questions/" data = { "title": "Why does the sun go down every night?", "content": "Why does the sun go down every night? I don't understand!", "tags": ["tagFirst", "tagSecond"], } request = self.factory.post(url, data) request.user = User.objects.get(username="******") view = QuestionAPI.as_view() response = view(request) msg_id = response.data.get("messageId") self.assertEqual( response.status_code, status.HTTP_201_CREATED, "Response status code %s was not 201" % response.status_code ) self.validate_question(response.data, data, request.user) # Edit question data_edited = { "title": "Why does the sun go down?", "content": "Why does the sun go down every night? I don't understand!", "messageId": msg_id, "tags": ["tagSecond", "tagThird"], } request = self.factory.post(url, data_edited) request.user = User.objects.get(username="******") response = view(request) # Edit existing question self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.validate_question(response.data, data_edited, request.user) # Test edited question attributes tags = response.data.get("tags") self.assertNotIn("tagFirst", tags, "tagFirst was in created question")
from django.contrib import admin admin.autodiscover() urlpatterns = patterns( '', url(r'^$', UserAPI.as_view()), url(r'^auth/login/?$', 'rest_framework.authtoken.views.obtain_auth_token', name="login"), url(r'^auth/load/?$', LoginAPI.as_view(), name="load"), url(r'^auth/logout/?$', LogoutAPI.as_view(), name="logout"), url(r'^answers/(?P<rid>[0-9]{1,9})/?$', AnswerAPI.as_view()), url(r'^answers/?$', AnswerAPI.as_view()), url( r'^comments/(?P<content_type>question|answer|comment)/(?P<rid>[0-9]{1,9})/?$', CommentAPI.as_view()), url( r'^votes/(?P<content_type>question|answer|comment)/(?P<rid>[0-9]{1,9})/?$', VoteAPI.as_view()), url(r'^questions/(?P<rid>[0-9]{1,9})/?$', QuestionAPI.as_view()), url(r'^questions/?$', QuestionAPI.as_view()), url(r'^tags/?$', TagAPI.as_view()), url(r'^organizations/(?P<organization_id>[0-9]{1,9})/?$', OrganizationAPI.as_view()), url(r'^organizations/?$', OrganizationAPI.as_view()), url(r'^courses/?$', CourseAPI.as_view()), url(r'^admin/', include(admin.site.urls)), url(r'^users/(?P<user_id>[0-9]{1,9})/?$', UserAPI.as_view()), url(r'^users/?$', UserAPI.as_view()), url(r'^reset/?$', ResetPasswordAPI.as_view()), url(r'^subscriptions/?$', SubscriptionAPI.as_view()))