Skip to content

Commit

Permalink
Fixing user_views_tests
Browse files Browse the repository at this point in the history
- Added DB annottion of pytest due to a conflit occurs after adding the
  sign in with google feature
  • Loading branch information
maayanhd committed May 11, 2021
1 parent c651be3 commit 53d4e73
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
25 changes: 11 additions & 14 deletions users/tests/test_allauth.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,26 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

from importlib import import_module
from requests.exceptions import HTTPError

from django.conf import settings
# from django.conf import settings
from django.contrib.auth.models import User
from django.core import mail
# from django.core import mail
from django.test.client import RequestFactory
from django.test import override_settings
from django.urls import reverse

from allauth.account import app_settings as account_settings
from allauth.account.adapter import get_adapter
# from allauth.account.adapter import get_adapter
from allauth.account.models import EmailAddress, EmailConfirmation
from allauth.account.signals import user_signed_up
from allauth.socialaccount.models import SocialAccount, SocialToken
from allauth.socialaccount.tests import OAuth2TestsMixin
from allauth.tests import MockedResponse, patch
from allauth.socialaccount.providers.google.provider import GoogleProvider
from pytest_django.asserts import assertTemplateUsed
import pytest

from unittest import mock


@pytest.mark.django_db
# Using this annotation to ensure what settings defined for the tests of google sign-in features in this class

class GoogleTests(OAuth2TestsMixin):
provider_id = GoogleProvider.id

Expand Down Expand Up @@ -60,7 +53,8 @@ def default_event_data_without_verified_email(family_name, name, email):
return {
"family_name": f'{family_name}',
"name": f'{name}',
"picture": "https://lh3.googleusercontent.com/a-/AOh14GhRHk2sL-xgCAYTscap9ByHf_16PYyebVwRkI6l1g=s96-c-rg-br100",
"picture": "https://lh3.googleusercontent.com/a-/A"
"Oh14GhRHk2sL-xgCAYTscap9ByHf_16PYyebVwRkI6l1g=s96-c-rg-br100",
"locale": "en-US",
"gender": "male",
"email": f'{email}',
Expand Down Expand Up @@ -119,7 +113,8 @@ def verified_mail_mocked_response(self, api_client, verified_email_event_data):
def unverified_mail_mocked_response(self, api_client, unverified_email_event_data):
return api_client.post('create-service', data=unverified_email_event_data)

# Imitating a response returned by google API by fabricating data partially and returning a json format string
# Imitating a response returned by google API by
# fabricating data partially and returning a json format string

@override_settings(
SOCIALACCOUNT_AUTO_SIGNUP=True,
Expand All @@ -141,7 +136,9 @@ def test_google_complete_login_401(self, api_client, event_data_401_code):
token = SocialToken(token="some_token")
# How can I connect the response to the process here without defining the mock response with
# patch and then assigning it as appeared here:
'''LessMockedResponse was a class that inherites from MockedResponse class and overriding/adding a raise_for_status method that raises HTTPError when the status code is NOT 200
'''LessMockedResponse was a class that inherites from MockedResponse
class and overriding/adding a raise_for_status method that raises HTTPError
when the status code is NOT 200-> here below:
response_with_401 = LessMockedResponse(
401,
"""
Expand All @@ -164,7 +161,7 @@ def test_google_complete_login_401(self, api_client, event_data_401_code):
response_with_401 = api_client.post('create-service', data=event_data_401_code)
with pytest.raises(HTTPError):
adapter.complete_login(request, app, token)
assert response.status_code == 401
assert response_with_401.status_code == 401

def test_username_based_on_email_address(
self, email, verified_mail_mocked_response
Expand Down
2 changes: 2 additions & 0 deletions users/tests/test_users_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ def test_profile_anonymous_GET(self, client):
response = client.get(reverse('profile'))
assert response.status_code == 302

@pytest.mark.django_db
def test_login_GET(self, client):
response = client.get(reverse('login'))
assert response.status_code == 200
assertTemplateUsed(response, 'users/login.html')

@pytest.mark.django_db
def test_logout_GET(self, client):
response = client.get(reverse('logout'))
assert response.status_code == 200
Expand Down

0 comments on commit 53d4e73

Please sign in to comment.