Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test : Create unit tests for User Info #94

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ export SENDGRID_API_KEY=<your-sendgrid-api-key>

## Testing

**Test Database:**
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
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
Expand Down
6 changes: 6 additions & 0 deletions osp/utils/zulip_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ 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
Expand Down
87 changes: 87 additions & 0 deletions tests/test_api_user_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
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": "[email protected]",
"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)
decon-harsh marked this conversation as resolved.
Show resolved Hide resolved
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)