-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1115 from jefer94/feature/accept-invites-thought-…
…of-an-api-rest accept an invite using an api rest
- Loading branch information
Showing
5 changed files
with
452 additions
and
314 deletions.
There are no files selected for viewing
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
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
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 |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
from unittest.mock import MagicMock, patch | ||
from django.template import loader | ||
from django.urls.base import reverse_lazy | ||
import pytest | ||
from rest_framework import status | ||
from django.http.request import HttpRequest | ||
from random import randint | ||
|
@@ -139,6 +140,28 @@ def salt(self): | |
return 'salt' | ||
|
||
|
||
def post_serializer(data={}): | ||
return { | ||
'created_at': ..., | ||
'email': None, | ||
'id': 0, | ||
'sent_at': None, | ||
'status': 'PENDING', | ||
**data, | ||
} | ||
|
||
|
||
created_at = None | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def setup(monkeypatch, utc_now): | ||
global created_at | ||
created_at = utc_now | ||
monkeypatch.setattr('django.utils.timezone.now', MagicMock(return_value=utc_now)) | ||
yield | ||
|
||
|
||
class AuthenticateTestSuite(AuthTestCase): | ||
"""Authentication test suite""" | ||
""" | ||
|
@@ -1020,3 +1043,88 @@ def test_member_invite_token__post__with_cohort__without_role(self): | |
]) | ||
|
||
self.assertEqual(self.bc.database.list_of('admissions.CohortUser'), []) | ||
|
||
""" | ||
🔽🔽🔽 POST JSON password is empty, UserInvite with email | ||
""" | ||
|
||
@patch('django.template.loader.render_to_string', MagicMock(side_effect=render_to_string_mock)) | ||
@patch('django.contrib.auth.hashers.get_hasher', MagicMock(side_effect=GetHasherMock)) | ||
@patch('django.db.models.signals.pre_delete.send', MagicMock(return_value=None)) | ||
@patch('breathecode.admissions.signals.student_edu_status_updated.send', MagicMock(return_value=None)) | ||
def test__post__json__password_is_empty(self): | ||
user_invite = {'email': '[email protected]'} | ||
model = self.bc.database.create(user_invite=user_invite) | ||
url = reverse_lazy('authenticate:member_invite_token', kwargs={'token': model.user_invite.token}) | ||
|
||
data = {'first_name': 'abc', 'last_name': 'xyz'} | ||
response = self.client.post(url, data, format='json') | ||
|
||
json = response.json() | ||
expected = {'detail': 'Password is empty', 'status_code': 400} | ||
|
||
self.assertEqual(json, expected) | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
self.assertEqual(self.bc.database.list_of('authenticate.UserInvite'), [ | ||
self.bc.format.to_dict(model.user_invite), | ||
]) | ||
|
||
self.assertEqual(self.bc.database.list_of('auth.User'), []) | ||
self.assertEqual(self.bc.database.list_of('authenticate.ProfileAcademy'), []) | ||
self.assertEqual(self.bc.database.list_of('admissions.CohortUser'), []) | ||
|
||
""" | ||
🔽🔽🔽 POST JSON with first name, last name and passwords, UserInvite with email | ||
""" | ||
|
||
@patch('django.template.loader.render_to_string', MagicMock(side_effect=render_to_string_mock)) | ||
@patch('django.contrib.auth.hashers.get_hasher', MagicMock(side_effect=GetHasherMock)) | ||
@patch('django.db.models.signals.pre_delete.send', MagicMock(return_value=None)) | ||
@patch('breathecode.admissions.signals.student_edu_status_updated.send', MagicMock(return_value=None)) | ||
def test__post__json__with_first_name_last_name_and_passwords(self): | ||
user_invite = {'email': '[email protected]'} | ||
model = self.bc.database.create(user_invite=user_invite) | ||
url = reverse_lazy('authenticate:member_invite_token', kwargs={'token': model.user_invite.token}) | ||
data = { | ||
'first_name': 'abc', | ||
'last_name': 'xyz', | ||
'password': '^3^3uUppppp', | ||
'repeat_password': '^3^3uUppppp', | ||
} | ||
response = self.client.post(url, data, format='json') | ||
|
||
json = response.json() | ||
expected = post_serializer({ | ||
'id': 1, | ||
'created_at': self.bc.datetime.to_iso_string(created_at), | ||
'status': 'ACCEPTED', | ||
'email': '[email protected]', | ||
}) | ||
|
||
self.assertEqual(json, expected) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(self.bc.database.list_of('authenticate.UserInvite'), | ||
[{ | ||
**self.bc.format.to_dict(model.user_invite), | ||
'status': 'ACCEPTED', | ||
'is_email_validated': True, | ||
}]) | ||
|
||
user_db = [ | ||
x for x in self.bc.database.list_of('auth.User') if x['date_joined'] and x.pop('date_joined') | ||
] | ||
self.assertEqual(user_db, [{ | ||
'email': '[email protected]', | ||
'first_name': 'abc', | ||
'id': 1, | ||
'is_active': True, | ||
'is_staff': False, | ||
'is_superuser': False, | ||
'last_login': None, | ||
'last_name': 'xyz', | ||
'password': CSRF_TOKEN, | ||
'username': '[email protected]' | ||
}]) | ||
|
||
self.assertEqual(self.bc.database.list_of('authenticate.ProfileAcademy'), []) | ||
self.assertEqual(self.bc.database.list_of('admissions.CohortUser'), []) |
Oops, something went wrong.