def test_create_sanity(self): response = api_test_request(method={"post": "create"}, view=views.UserViewset, user=self.super_usr, data={ "email": "*****@*****.**", "password": "******", "first_name": "abcde", "last_name": "bcd", "phone": 1234567891, "pincode": 123456 }) self.assertEqual(response.status_code, 400) response = api_test_request(method={"post": "create"}, view=views.UserViewset, user=self.super_usr, data={ "email": "*****@*****.**", "password": "******", "first_name": "abcde", "last_name": "bcd", "phone": 1234567891, "pincode": 123456 }) self.assertEqual(response.status_code, 201) self.assertEqual(response.data['email'], "*****@*****.**")
def test_getlist_sanity(self): response = api_test_request(method={"get": "list"}, view=views.UserViewset, user=self.usr) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1) response = api_test_request(method={"get": "list"}, view=views.UserViewset, user=self.super_usr) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 2)
def test_getlist_sanity(self): response = api_test_request(method={"get": "list"}, view=views.ArticleViewset, user=self.usr) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1) # Check if the super user is shown all the articles and not just the one's he created response = api_test_request(method={"get": "list"}, view=views.ArticleViewset, user=self.super_usr) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 2)
def test_download_sanity(self): response = api_test_request(view=views.ArticleDownload, user=self.usr, format='multipart', id='1') self.assertEqual(response.status_code, 200) self.assertEqual(response.content, b'binary data') response = api_test_request(view=views.ArticleDownload, user=self.usr, format='multipart', id='2') self.assertEqual(response.status_code, 404) self.assertEqual(response.data['detail'], 'Not found.')
def test_delete_sanity(self): response = api_test_request(method={"delete": "destroy"}, view=views.ArticleViewset, user=self.usr, id='1') self.assertEqual(response.status_code, 204)
def test_getdetail_sanity(self): response = api_test_request(method={"get": "retrieve"}, view=views.ArticleViewset, user=self.usr, id='1') self.assertEqual(response.status_code, 200) self.assertEqual(response.data['title'], 'sample title') # Check if the super user is able to read some other user's article response = api_test_request(method={"get": "retrieve"}, view=views.ArticleViewset, user=self.super_usr, id='1') self.assertEqual(response.status_code, 200) self.assertEqual(response.data['title'], 'sample title')
def test_getdetail_sanity(self): response = api_test_request(method={"get": "retrieve"}, view=views.UserViewset, user=self.usr, pk=self.usr.pk) self.assertEqual(response.status_code, 200) self.assertEqual(response.data['email'], 'dummy_mail') # Check if the super user is able to read some other user's record response = api_test_request(method={"get": "retrieve"}, view=views.UserViewset, user=self.super_usr, pk=self.super_usr.pk) self.assertEqual(response.status_code, 200) self.assertEqual(response.data['email'], 'dummy_smail')
def test_query_title(self): response = api_test_request(view=views.ArticleQuery, user=self.super_usr, query_string="?title=javascript") self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 2) self.assertEqual(response.data[0]['title'], "Understanding Inheritance in JavaScript")
def test_query_categories(self): response = api_test_request( view=views.ArticleQuery, user=self.usr, query_string="?categories=sample_category2") self.assertEqual(response.status_code, 200) # Check if the response doesn't have the article created by superuser as the query is being made by normal user self.assertEqual(len(response.data), 1)
def test_invalid_creds(self): response = api_test_request(method="post", view=views.Login, data={ "email": "dummy_mail", "password": "******" }) self.assertEqual(response.status_code, 401)
def test_login_sanity(self, mock_login): mock_login.return_value = True response = api_test_request(method="post", view=views.Login, data={ "email": "dummy_mail", "password": "******" }) self.assertEqual(response.status_code, 200)
def test_update_sanity(self): response = api_test_request(method={"put": "update"}, view=views.UserViewset, user=self.usr, data={ "email": "*****@*****.**", "password": "******", "first_name": "abcde", "last_name": "bcd", "phone": 1234567891, "pincode": 123456 }, pk=self.usr.pk) self.assertEqual(response.status_code, 200) self.assertEqual(response.data['email'], "*****@*****.**")
def test_update_sanity(self): response = api_test_request(method={"put": "update"}, view=views.ArticleViewset, user=self.usr, data={ "title": "sample user title2", "body": "sample user body2", "summary": "sample summary2", "file": SimpleUploadedFile( "hr2.pdf", b'binary data', content_type='application/pdf'), "categories": "" }, id='1', format="multipart") self.assertEqual(response.status_code, 200) self.assertEqual(response.data['title'], "sample user title2")