From 9c66a982fd10e16196422d3e4fe0347151052d4b Mon Sep 17 00:00:00 2001 From: decon-harsh Date: Mon, 8 Mar 2021 03:08:28 +0530 Subject: [PATCH 1/2] Test:Create unit tests for User Info --- README.md | 11 ++++- osp/utils/zulip_api.py | 3 ++ tests/test_api_user_info.py | 86 +++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 tests/test_api_user_info.py diff --git a/README.md b/README.md index bda31a5..3fdda39 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ Make sure you have installed the following: Next follow these instructions. -1. **Database Setup:** Before starting with the project create a db in you local using PostgreSQL with the following details. Refer to `main/settings.py` if you have any confusion. +1.1 **Database Setup:** Before starting with the project create a db in you local using PostgreSQL with the following details. Refer to `main/settings.py` if you have any confusion. ``` NAME: osp @@ -126,6 +126,15 @@ export SENDGRID_API_KEY= ## Testing +**Test Database:** + You may run the following commands: + + ``` + cd open-source-programs-backend + sudo -i -u postgres + GRANT ALL PRIVILEGES ON DATABASE test_osp to osp; + ALTER USER osp CREATEDB; + ``` To run the tests run: `python manage.py test`. ## QA Checks diff --git a/osp/utils/zulip_api.py b/osp/utils/zulip_api.py index 0fa54cc..108204b 100644 --- a/osp/utils/zulip_api.py +++ b/osp/utils/zulip_api.py @@ -4,6 +4,9 @@ # this file one directory previous to the cuurent directory your file is in client = Client(config_file="download") +def get_self_zulip_id(): + result = client.get_profile() + return result["user_id"] def get_zulip_user(zulip_id): result = client.get_user_by_id(zulip_id) diff --git a/tests/test_api_user_info.py b/tests/test_api_user_info.py new file mode 100644 index 0000000..0396b32 --- /dev/null +++ b/tests/test_api_user_info.py @@ -0,0 +1,86 @@ +from django.contrib.auth import get_user_model +from rest_framework import status +from rest_framework.test import APIClient, APITestCase +from osp.utils.zulip_api import get_self_zulip_id + +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) + + self.zulip_id = get_self_zulip_id() + + 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": self.zulip_id} + response = self.client.post("http://localhost:8000/api/info/", body, format="json") + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + self.assertEqual(response.data["name"], body["name"]) + self.assertEqual(response.data["user_type"], body["user_type"]) + self.assertEqual(response.data["zulip_id"], body["zulip_id"]) + self.assertEqual(response.data["user"]["email"], self.register_data["email"]) + self.assertEqual(response.data["user"]["username"], self.register_data["username"]) + + def test_get_user_info_successfully(self): + + body = {"name": "Test User 1 Full Name", "user_type": "admin", "zulip_id": self.zulip_id} + response = self.client.post("http://localhost:8000/api/info/", body, format="json") + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + self.assertNotEqual(response.data,[]) + + response = self.client.get("http://localhost:8000/api/info/", format="json") + response.data = response.data[0] + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertNotEqual(response.data,[]) + self.assertEqual(response.data["name"], body["name"]) + self.assertEqual(response.data["user_type"], body["user_type"]) + self.assertEqual(response.data["zulip_id"], body["zulip_id"]) + self.assertEqual(response.data["user"]["email"], self.register_data["email"]) + self.assertEqual(response.data["user"]["username"], self.register_data["username"]) + + 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": self.zulip_id} + 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) From 129064c0b71b1f34dd64faf9f8b39b6aa80264ce Mon Sep 17 00:00:00 2001 From: Harsh Singh Date: Mon, 8 Mar 2021 03:35:14 +0530 Subject: [PATCH 2/2] Update README.md --- README.md | 4 ++-- osp/utils/zulip_api.py | 9 ++++++--- tests/test_api_user_info.py | 7 ++++--- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 0e2084f..dffbd86 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ Make sure you have installed the following: Next follow these instructions. -1.1 **Database Setup:** Before starting with the project create a db in you local using PostgreSQL with the following details. Refer to `main/settings.py` if you have any confusion. +1. **Database Setup:** Before starting with the project create a db in you local using PostgreSQL with the following details. Refer to `main/settings.py` if you have any confusion. ``` NAME: osp @@ -139,7 +139,7 @@ export SENDGRID_API_KEY= ## Testing **Test Database:** - You may run the following commands: + For creating a database while testing you have to give database creation permission to user `osp`. You may run the following commands: ``` cd open-source-programs-backend diff --git a/osp/utils/zulip_api.py b/osp/utils/zulip_api.py index 916b160..95cc2b1 100644 --- a/osp/utils/zulip_api.py +++ b/osp/utils/zulip_api.py @@ -3,14 +3,17 @@ # By default the API key file you download is named as 'download' by Zulip. You can place # this file one directory previous to the cuurent directory your file is in -def get_self_zulip_id(): - result = client.get_profile() - return result["user_id"] def get_client(): return Client(config_file="download") +def get_self_zulip_id(): + client = get_client() + result = client.get_profile() + return result["user_id"] + + def get_zulip_user(zulip_id: int) -> dict: """Takes the zulip user ID as an argument and returns a dictionary containing basic data on the Zulip user associated with that id diff --git a/tests/test_api_user_info.py b/tests/test_api_user_info.py index 0396b32..d398bdf 100644 --- a/tests/test_api_user_info.py +++ b/tests/test_api_user_info.py @@ -1,6 +1,7 @@ from django.contrib.auth import get_user_model from rest_framework import status from rest_framework.test import APIClient, APITestCase + from osp.utils.zulip_api import get_self_zulip_id User = get_user_model() @@ -58,12 +59,12 @@ def test_get_user_info_successfully(self): body = {"name": "Test User 1 Full Name", "user_type": "admin", "zulip_id": self.zulip_id} response = self.client.post("http://localhost:8000/api/info/", body, format="json") self.assertEqual(response.status_code, status.HTTP_201_CREATED) - self.assertNotEqual(response.data,[]) - + self.assertNotEqual(response.data, []) + response = self.client.get("http://localhost:8000/api/info/", format="json") response.data = response.data[0] self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertNotEqual(response.data,[]) + self.assertNotEqual(response.data, []) self.assertEqual(response.data["name"], body["name"]) self.assertEqual(response.data["user_type"], body["user_type"]) self.assertEqual(response.data["zulip_id"], body["zulip_id"])