From 42245fe7de5138b5efe67289aa67d9e5857f3dc2 Mon Sep 17 00:00:00 2001 From: decon-harsh Date: Sun, 21 Feb 2021 02:03:48 +0530 Subject: [PATCH] Test : Create unit tests for User Info --- README.md | 2 ++ main/settings.py | 1 + tests/test_api_user_info.py | 66 +++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 tests/test_api_user_info.py diff --git a/README.md b/README.md index 2a2bd6d..c1ff45e 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,8 @@ Next follow these instructions. CREATE DATABASE osp; \c osp; GRANT ALL PRIVILEGES ON DATABASE osp to osp; + GRANT ALL PRIVILEGES ON DATABASE test_osp to osp; + ALTER USER osp CREATEDB; ``` 2. **Set the environment variables:** You need to download Zulip API key file from your user-settings on Zulip. The file you download is named as `download` or rename it to `download`. Place that download file in the project's directory. For more information follow [Environment Variables](#Environment-Variables) section. diff --git a/main/settings.py b/main/settings.py index a33ec94..fe6b161 100644 --- a/main/settings.py +++ b/main/settings.py @@ -84,6 +84,7 @@ "rest_framework.authentication.SessionAuthentication", "rest_framework_simplejwt.authentication.JWTAuthentication", ), + "TEST_REQUEST_DEFAULT_FORMAT": "json", } SIMPLE_JWT = { diff --git a/tests/test_api_user_info.py b/tests/test_api_user_info.py new file mode 100644 index 0000000..54fe9e4 --- /dev/null +++ b/tests/test_api_user_info.py @@ -0,0 +1,66 @@ +from django.contrib.auth import get_user_model +from rest_framework import status +from rest_framework.test import APIClient, APITestCase + +User = get_user_model() + + +class UserInfoTests(APITestCase): + def setUp(self): + + # Needed for token auths + self.client = APIClient() + + # Register + self.register_data = { + "username": "testuser1", + "email": "testuser1@gmail.com", + "password": "hello", + "confirm_password": "hello", + } + test_user = User(username=self.register_data["username"], email=self.register_data["email"], is_active=True) + test_user.set_password(self.register_data["password"]) + test_user.save() + + # Login and get real token + login_data = {"username": self.register_data["username"], "password": self.register_data["password"]} + response = self.client.post( + "http://localhost:8000/api/token_auth/token/", + login_data, + format="json", + headers={"Content-Type": "application/json"}, + ) + self.access_token = response.json()["access"] + self.client.credentials(HTTP_AUTHORIZATION="Bearer " + self.access_token) + + def test_get_user_info_successfully_empty(self): + + response = self.client.get("http://localhost:8000/api/info/", format="json") + self.assertEqual(response.data, []) + self.assertEqual(response.status_code, status.HTTP_200_OK) + + def test_post_user_info_successfully(self): + + body = {"name": "Test User 1 Full Name", "user_type": "admin", "zulip_id": 334084} + response = self.client.post("http://localhost:8000/api/info/", body, format="json") + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + + def test_get_user_info_successfully(self): + + response = self.client.get("http://localhost:8000/api/info/", format="json") + self.assertEqual(response.status_code, status.HTTP_200_OK) + + def test_api_wrong_token(self): + + self.client.credentials() + response = self.client.get("http://localhost:8000/api/info/", format="json") + self.assertEqual(response.data["detail"], "Authentication credentials were not provided.") + self.assertEqual(response.status_code, 403) + + def test_multiple_post_user_info(self): + + self.client.credentials(HTTP_AUTHORIZATION="Bearer " + self.access_token) + body = {"name": "Test User 1 Full Name", "user_type": "admin", "zulip_id": 334084} + for _ in range(2): + response = self.client.post("http://localhost:8000/api/info/", body, format="json") + self.assertEqual(response.status_code, status.HTTP_409_CONFLICT)