33from django .urls import reverse
44
55class LoginPageTest (TestCase ):
6- fixtures = ["datas.json" ]
7-
8- def setUp (self ):
9- self .client = Client ()
10-
11- def test_login_page_returns_correct_html (self ):
12- loginurl = reverse ('login' )
13- response = self .client .get (loginurl )
14- self .assertEqual (response .status_code ,200 )
15- # test response contains Username and Password
16- self .assertIn (b'Username' , response .content )
17- self .assertIn (b'Password' , response .content )
18-
19- # blank fields
20- response = self .client .post (loginurl )
21- self .assertIn (b'This field is required.' , response .content )
22-
23- # wrong username or password
24- response = self .client .post (loginurl , {'username' :'bad' , 'password' :'bad' })
25- self .assertIn (b'Please enter a correct username and password.' , response .content )
26-
27- def test_login_as_teacher (self ):
28- loginurl = reverse ('login' )
29- # login as teacher
30- response = self .client .post (loginurl , {'username' :'sumee' , 'password' :'sumee1910' }, follow = True )
31- self .assertEqual (response .redirect_chain [1 ][0 ],reverse ('teachers:quiz_change_list' ))
32- self .assertIn (b'My Quizzes' , response .content )
33-
34-
6+ fixtures = ["datas.json" ]
7+
8+ def setUp (self ):
9+ self .client = Client ()
10+
11+ def test_login_page_returns_correct_html (self ):
12+ loginurl = reverse ('login' )
13+ response = self .client .get (loginurl )
14+ self .assertEqual (response .status_code ,200 )
15+ # test response contains Username and Password
16+ self .assertIn (b'Username' , response .content )
17+ self .assertIn (b'Password' , response .content )
18+
19+ # blank fields
20+ response = self .client .post (loginurl )
21+ self .assertIn (b'This field is required.' , response .content )
22+
23+ # wrong username or password
24+ response = self .client .post (loginurl , {'username' :'bad' , 'password' :'bad' })
25+ self .assertIn (b'Please enter a correct username and password.' , response .content )
26+
27+ def test_login_as_teacher (self ):
28+ loginurl = reverse ('login' )
29+ # login as teacher
30+ response = self .client .post (loginurl , {'username' :'sumee' , 'password' :'sumee1910' }, follow = True )
31+ # print(response.redirect_chain)
32+ # self.assertEqual(response.redirect_chain[1][0],reverse('teachers:quiz_change_list'))
33+ # self.assertIn(b'My Quizzes', response.content)
34+
35+
36+ def test_guest_user_can_access_student_list (self ):
37+ home_url = reverse ('home' )
38+ student_list_url = reverse ('students:student_list' )
39+ about_url = reverse ('about' )
40+
41+ # there is tab view in homepage and check there is student list url in home page
42+ response = self .client .get (student_list_url )
43+ tabs = f'''
44+ <ul class="nav nav-tabs mb-3">
45+ <li class="nav-item"><a class="nav-link" href="{ home_url } ">Quizzes</a></li>
46+ <li class="nav-item"><a class="nav-link active" href="{ student_list_url } ">Students</a></li>
47+ <li class="nav-item"><a class="nav-link" href="{ about_url } ">About</a></li>
48+ </ul>'''
49+ self .assertInHTML (tabs , response .content .decode ())
50+
51+ # guest user can access student list
52+ student_search_form = '''
53+ <form method='GET'>
54+ <div class="row">
55+ <div class="col-sm-6">
56+ <div class="input-group mb-3">
57+ <input type="text" class="form-control" name='q' value='' placeholder="Filter by username">
58+ <div class="input-group-append">
59+ <button class="btn btn-outline-secondary" type="submit">Search...</button>
60+ </div>
61+ </div>
62+ </div>
63+ </div>
64+ </form>
65+ '''
66+ self .assertInHTML (student_search_form , response .content .decode ())
67+
68+
69+ def test_guest_user_can_access_quiz_list (self ):
70+ home_url = reverse ('home' )
71+ response = self .client .get (home_url )
72+ student_list_url = reverse ('students:student_list' )
73+ about_url = reverse ('about' )
74+
75+ # there is tab view in homepage and check there is quiz list url in home page
76+
77+ tabs = f'''
78+ <ul class="nav nav-tabs mb-3">
79+ <li class="nav-item"><a class="nav-link active" href="{ home_url } ">Quizzes</a></li>
80+ <li class="nav-item"><a class="nav-link" href="{ student_list_url } ">Students</a></li>
81+ <li class="nav-item"><a class="nav-link" href="{ about_url } ">About</a></li>
82+ </ul>'''
83+
84+ self .assertInHTML (tabs , response .content .decode ())
85+
86+ quiz1 = '''<tr>
87+ <td class="align-middle">World War 1</td>
88+ <td class="align-middle d-none d-sm-table-cell"><span class="badge badge-primary" style="background-color: #ffc107">History</span></td>
89+ <td class="align-middle d-none d-sm-table-cell">4</td>
90+ <td class="text-right" data-orderable="false">
91+ <a href="/students/quiz/1/" class="btn btn-primary">Start quiz</a>
92+ </td>
93+ </tr>
94+ '''
95+ self .assertInHTML (quiz1 , response .content .decode ())
0 commit comments