Skip to content

Commit

Permalink
Test : Create unit tests for User Info
Browse files Browse the repository at this point in the history
  • Loading branch information
decon-harsh committed Feb 23, 2021
1 parent 11d0c3c commit 41d6da1
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
12 changes: 11 additions & 1 deletion 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. **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
Expand All @@ -45,6 +45,7 @@ Next follow these instructions.
\c osp;
GRANT ALL PRIVILEGES ON DATABASE osp to osp;
```

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.

3. Move into the project's directory.
Expand Down Expand Up @@ -102,6 +103,15 @@ export SENDGRID_API_KEY=<your-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
Expand Down
76 changes: 76 additions & 0 deletions tests/test_api_user_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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": "[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)

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):

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)

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.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": 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)

0 comments on commit 41d6da1

Please sign in to comment.