This repository has been archived by the owner on Sep 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests to ensure that create new accounts is called properly
- Loading branch information
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |