Skip to content
This repository has been archived by the owner on Sep 12, 2022. It is now read-only.

Commit

Permalink
Add tests to ensure that create new accounts is called properly
Browse files Browse the repository at this point in the history
  • Loading branch information
cdosborn committed Sep 6, 2018
1 parent 5823391 commit 938ee0b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
Empty file added api/tests/v1/__init__.py
Empty file.
44 changes: 44 additions & 0 deletions api/tests/v1/test_profile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from django.test import TestCase, override_settings
from django.urls import reverse
from rest_framework.test import APIRequestFactory, force_authenticate
import mock

from api.v1.views import Profile
from api.tests.factories import UserFactory
from core.models import AtmosphereUser

class ProfileTests(TestCase):
@override_settings(AUTO_CREATE_NEW_ACCOUNTS=True)
def test_external_accounts_are_created(self):
"""
Sanity check that configuration results in call to create_new_accounts.
"""
user = UserFactory()
url = reverse('api:v1:profile')
view = Profile.as_view()
factory = APIRequestFactory()
request = factory.get(url)
force_authenticate(request, user=user)

with mock.patch("api.v1.views.profile.create_new_accounts") as mock_create_new_accounts:
response = view(request)
mock_create_new_accounts.assert_called_once()

@override_settings(AUTO_CREATE_NEW_ACCOUNTS=True)
def test_external_accounts_are_not_created_for_invalid_user(self):
"""
Accounts are NOT created when when the user is invalid
"""
user = UserFactory()
url = reverse('api:v1:profile')
view = Profile.as_view()
factory = APIRequestFactory()
request = factory.get(url)
force_authenticate(request, user=user)

# Patch the user so that they are invalid
with mock.patch.object(AtmosphereUser, 'is_valid', return_value=False), \
mock.patch("api.v1.views.profile.create_new_accounts") \
as mock_create_new_accounts:
response = view(request)
mock_create_new_accounts.assert_not_called()

0 comments on commit 938ee0b

Please sign in to comment.