Skip to content

Commit

Permalink
Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
decon-harsh committed Mar 7, 2021
1 parent c705abd commit dd21215
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -127,7 +127,7 @@ export SENDGRID_API_KEY=<your-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
Expand Down
3 changes: 3 additions & 0 deletions osp/utils/zulip_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 14 additions & 4 deletions tests/test_api_user_info.py
Original file line number Diff line number Diff line change
@@ -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()

Expand Down Expand Up @@ -33,6 +34,8 @@ def setUp(self):
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")
Expand All @@ -41,19 +44,26 @@ def test_get_user_info_successfully_empty(self):

def test_post_user_info_successfully(self):

body = {"name": "Test User 1 Full Name", "user_type": "admin", "zulip_id": 334084}
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": 334084}
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"])
Expand All @@ -70,7 +80,7 @@ def test_api_wrong_token(self):
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}
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)

0 comments on commit dd21215

Please sign in to comment.