-
Notifications
You must be signed in to change notification settings - Fork 19
/
tests.py
66 lines (56 loc) · 2.1 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import logging
import os
import unittest
from unittest.mock import Mock, patch
from django.core import mail
from django.test import TestCase
from django.urls import reverse
from apps.ldap.test_helpers import LDAPTestCase
from apps.ldap.utils import email_exists
from apps.newuser.views import newuser_script
from .tokens import newuser_token_generator
class NewUserTest(LDAPTestCase):
"""
Tests the LDAP code by mocking the CSUA LDAP server.
"""
# TODO: include tests for failure modes (newuser bind fail, config_newuser
# fails)
@patch("subprocess.run")
def test_remote_newuser_flow(self, subprocess_run):
logging.disable(logging.CRITICAL)
url = "/newuser/remote/"
email = "[email protected]"
resp = self.client.get(url)
resp = self.client.post(url, {"email": email})
self.assertContains(resp, "Email sent")
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(mail.outbox[0].to, [email])
token_url = reverse(
"newuser-remote",
kwargs={"email": email, "token": newuser_token_generator.make_token(email)},
)
full_url = "https://www.csua.berkeley.edu" + token_url
self.assertIn(full_url, mail.outbox[0].body)
subprocess_run.return_value.returncode = 0
self.assertFalse(email_exists(email))
resp = self.client.get(token_url)
resp = self.client.post(
token_url,
{
"full_name": "Phillip E. Nunez II",
"student_id": 3114201612,
"email": email,
"username": "pnunez2",
"password": "okPASSWORD1!",
"enroll_jobs": False,
"agree_rules": True,
},
)
self.assertEqual(len(subprocess_run.call_args_list), 1)
self.assertNotContains(resp, "failed")
args = subprocess_run.call_args_list[0][0][0]
self.assertIs(type(args), list)
for arg in args:
self.assertIs(type(arg), str)
self.assertTrue(email_exists(email))
logging.disable(logging.NOTSET)