Skip to content

Commit

Permalink
added tests and fixed name
Browse files Browse the repository at this point in the history
  • Loading branch information
codesankalp committed Mar 7, 2021
1 parent e158599 commit 7e96fdc
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 8 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ jobs:
pip install -r requirements.txt
pip install coverage==5.4
- name: Exporting Secret Key
run: echo -e "SECRET_KEY=${{ secrets.SECRET_KEY }}" >> .env

- name: Migrating Test Database
env:
SECRET_KEY: 2ry4u5$v3x%#9q@s@r4m0cbvd$s
run: python manage.py migrate

- name: Run tests
env:
SECRET_KEY: 2ry4u5$v3x%#9q@s@r4m0cbvd$s
run: coverage run manage.py test

- name: Generate coverage report
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export SENDGRID_API_KEY=<your-sendgrid-api-key>
3. Go to **Sites dashboard** in admin site. (**URL**: http://localhost:8000/admin/sites/site/).
4. Click on `Add site` button and fill in the information as given in the image.
![site_id](https://user-images.githubusercontent.com/56037184/109974910-0fa79b00-7d20-11eb-9826-44fdf6d770f9.png)
**Note**: After saving this if the site id is not `2` then change the `SITE_ID` in settings.py with the new site id.
**Note**: After saving this if the site id is not `1` then change the `SITE_ID` in settings.py with the new site id.
5. After this go to **Social Applications Dashboard**. (**URL**: http://localhost:8000/admin/socialaccount/socialapp/).
6. Add the credentials that you get after creating the GitHub app. Fill in the information as given in the image.
![social_add](https://user-images.githubusercontent.com/56037184/109975941-35816f80-7d21-11eb-9a8f-205953306c83.png)
Expand Down
3 changes: 2 additions & 1 deletion main/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
SECRET_KEY = os.environ["SECRET_KEY"]
else:
raise ValueError("Secret Key can't be empty or None.")

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

Expand Down Expand Up @@ -68,7 +69,7 @@
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]

SITE_ID = 2
SITE_ID = 1

ROOT_URLCONF = "main.urls"

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pytz==2020.1
regex==2020.11.13
requests==2.24.0
requests-oauthlib==1.3.0
responses==0.12.1
sendgrid==6.3.1
six==1.15.0
sqlparse==0.3.1
Expand Down
73 changes: 73 additions & 0 deletions tests/test_github_oauth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import json

import responses
from allauth.socialaccount.models import SocialApp
from django.contrib.auth import get_user_model
from django.contrib.sites.models import Site
from django.test import TestCase
from django.urls import reverse
from rest_framework.test import APIClient


class TestGitHubAuth(TestCase):
"""
Tests for Github Authentication.
"""

def setUp(self):
self.client = APIClient()
self.github_login_url = reverse("github_login")
self.payload = {"code": "123456"}
social_app = SocialApp.objects.create(
provider="github",
name="Github",
client_id="123123123",
secret="321321321",
)
site = Site.objects.get_current()
social_app.sites.add(site)

@responses.activate
def test_github_auth(self):
"""
Testing GitHub Auth Registration and Login.
"""
# fake responses to mock GitHub API call
self.__add_github_responses()

users_count = get_user_model().objects.all().count()

response = self.client.post(self.github_login_url, data=self.payload, format="json")

self.assertEquals(response.status_code, 200)
# assertion to check new user is created.
self.assertEquals(get_user_model().objects.all().count(), users_count + 1)

# Below two assertions are added to check REST_USE_JWT is True or not.
self.assertIn("access_token", response.data)
self.assertIn("refresh_token", response.data)

# Assertion to check second request will not create a new user
response = self.client.post(self.github_login_url, data=self.payload, format="json")
self.assertEquals(get_user_model().objects.all().count(), users_count + 1)

def __add_github_responses(self):
"""
Private methods to add responses for different GitHub API Calls.
"""

responses.add(
responses.POST,
"https://github.com/login/oauth/access_token",
body=json.dumps({"access_token": "access_token"}),
status=200,
content_type="application/json",
)

responses.add(
responses.GET,
"https://api.github.com/user",
body=json.dumps({"id": "123456", "login": "TestUser", "name": "Test User", "type": "User"}),
status=200,
content_type="application/json",
)
4 changes: 2 additions & 2 deletions token_auth/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.urls import path
from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView

from token_auth.views.github_oauth import GithubLogin
from token_auth.views.github_oauth import GitHubLogin
from token_auth.views.register import RegisterView

urlpatterns = [
Expand All @@ -15,5 +15,5 @@
# login URLs
path("token/", TokenObtainPairView.as_view(), name="token_obtain_pair"),
path("refresh/", TokenRefreshView.as_view(), name="token_refresh"),
path("github/", GithubLogin.as_view(), name="github_login"),
path("github/", GitHubLogin.as_view(), name="github_login"),
]
2 changes: 1 addition & 1 deletion token_auth/views/github_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.conf import settings


class GithubLogin(SocialLoginView):
class GitHubLogin(SocialLoginView):
authentication_classes = []
adapter_class = GitHubOAuth2Adapter
callback_url = settings.GITHUB_CALLBACK_URL
Expand Down

0 comments on commit 7e96fdc

Please sign in to comment.