1
1
# Copyright 2022 UW-IT, University of Washington
2
2
# SPDX-License-Identifier: Apache-2.0
3
3
4
- import json
5
- from myuw .test .api import missing_url , MyuwApiTest
4
+ from unittest . mock import patch
5
+ from myuw .test .api import MyuwApiTest
6
6
from django .test .client import RequestFactory
7
7
from myuw .models .myuw_notice import MyuwNotice
8
- from myuw .views .notice_admin import _get_datetime , _save_notice
9
- from datetime import datetime
8
+ from myuw .views .notice_admin import _get_datetime , _get_html , _save_notice
10
9
from myuw .dao .myuw_notice import get_myuw_notices_for_user
11
10
from myuw .test import get_request_with_user , get_request_with_date
12
- from myuw .test .dao .test_myuw_notice import get_datetime_with_tz
13
11
12
+ CONTENT = (
13
+ "<p>Your Husky card is your key to efficiency on " +
14
+ "campus and serves as your University ID, providing access " +
15
+ "to eligible services such as dining plans, facilities, " +
16
+ "transportation, UW libraries, and so much more.</p>\n <p><a " +
17
+ "href=\" http://hfs.uw.edu/Husky-Card-Services/submit-a-photo" +
18
+ "\" >Submit your photo online</a>\n to be able to have your " +
19
+ "Husky card printed and sent to you by mail. Students with " +
20
+ "an international address should submit a photo online and " +
21
+ "plan to pick up their Husky card on campus. Visit the <a " +
22
+ "href=\" https://hfs.uw.edu/Husky-Card-Services/Operations-" +
23
+ "Updates\" title=\" https://hfs.uw.edu/Husky-Card-Services/" +
24
+ "Operations-Updates\" >Husky Card Operations</a> page for " +
25
+ "more information.\n </p>" )
14
26
15
- class TestNoticeAdmin (MyuwApiTest ):
16
27
17
- def test_get_datetime (self ):
18
- string = ""
19
- self .assertIsNone (_get_datetime (string ))
28
+ class TestNoticeAdmin (MyuwApiTest ):
20
29
21
- string = "foobar"
22
- self . assertIsNone ( _get_datetime ( string ) )
30
+ def _get_request ( self , notice ):
31
+ return RequestFactory (). post ( '' , notice )
23
32
33
+ def test_get_datetime (self ):
34
+ self .assertIsNone (_get_datetime ("" ))
35
+ self .assertIsNone (_get_datetime (None ))
24
36
string = "2018-05-08 15:28"
25
37
self .assertEqual (str (_get_datetime (string )),
26
38
"2018-05-08 15:28:00-07:00" )
39
+ string = "2018-05-08T15:28"
40
+ self .assertEqual (str (_get_datetime (string )),
41
+ "2018-05-08 15:28:00-07:00" )
42
+ string = "2018-05-08T15:28:00-07:00"
43
+ self .assertEqual (str (_get_datetime (string )),
44
+ "2018-05-08 15:28:00-07:00" )
27
45
28
- string = "2013-03-28 10:00:00+00:00"
29
- dt = get_datetime_with_tz (2013 , 3 , 28 , 3 )
30
- self .assertEqual (_get_datetime (string ), dt )
46
+ def test_get_html (self ):
47
+ self .assertIsNone (_get_html (None ))
48
+ self .assertEqual (_get_html ('' ), "" )
49
+ self .assertIsNotNone (_get_html (CONTENT ))
50
+
51
+ @patch .object (MyuwNotice , 'save' )
52
+ def test_sql_error_save (self , mock ):
53
+ request = self ._get_request (
54
+ {
55
+ 'action' : 'save' ,
56
+ "title" : "Test" ,
57
+ "start_date" : "2022-04-12 11:25" ,
58
+ "end_date" : "2022-04-15 11:25" ,
59
+ "content" : CONTENT ,
60
+ "notice_type" : "Banner" ,
61
+ "notice_category" : "MyUWNotice" ,
62
+ "is_critical" : False ,
63
+ 'affil' : ['is_employee' ],
64
+ 'target_group' : ''
65
+ })
66
+ context = {}
67
+ mock .side_effect = Exception ('Error!' )
68
+ self .assertFalse (_save_notice (request , context ))
69
+ self .assertTrue (context ['sql_error' ])
31
70
32
71
def test_save_new_notice (self ):
33
- rf = RequestFactory ()
34
- request = rf .post ('' , {})
35
-
72
+ request = self ._get_request ({})
36
73
saved = _save_notice (request , {})
37
74
self .assertFalse (saved )
38
75
39
- notice_context = {
40
- 'action' : 'save' ,
41
- 'title ' : 'The Title ' ,
42
- 'content ' : "<p>Foobar</p>" ,
43
- 'affil ' : 'is_intl_stud' ,
44
- 'campus ' : 'is_seattle ' ,
45
- 'start_date ' : '2018-05-25 12:00 ' ,
46
- 'end_date ' : '2018-05-26 12:00' ,
47
- 'notice_type ' : 'Foo ' ,
48
- 'notice_category ' : 'Bar ' ,
49
- 'target_group ' : ' uw_group '
50
- }
51
- request = rf . post ( '' , notice_context )
76
+ request = self . _get_request (
77
+ {
78
+ 'action ' : 'save ' ,
79
+ 'title ' : 'The Title ' ,
80
+ 'content ' : "<p>Foobar</p>" ,
81
+ 'affil ' : 'is_intl_stud ' ,
82
+ 'campus ' : 'is_seattle ' ,
83
+ 'start_date ' : '2018-05-25 12:00' ,
84
+ 'end_date ' : '2018-05-26 12:00 ' ,
85
+ 'notice_type ' : 'Foo ' ,
86
+ 'notice_category ' : 'Bar' ,
87
+ 'target_group' : ' uw_group '
88
+ } )
52
89
self .assertTrue (_save_notice (request , {}))
53
90
54
91
entries = MyuwNotice .objects .all ()
@@ -63,84 +100,80 @@ def test_save_new_notice(self):
63
100
self .assertEqual (entries [0 ].target_group , "uw_group" )
64
101
65
102
# end before start
66
- notice_context = {
67
- 'action' : 'save' ,
68
- 'title ' : 'The Title ' ,
69
- 'content ' : "<p>Foobar</p>" ,
70
- 'start_date ' : "2018-05-25T12:05:00+00:00 " ,
71
- 'end_date ' : "2017 -05-26T12 :05:00+00:00 " ,
72
- 'notice_type ' : 'Foo' ,
73
- 'notice_category ' : 'Bar'
74
- }
75
- request = rf . post ( '' , notice_context )
103
+ request = self . _get_request (
104
+ {
105
+ 'action ' : 'save ' ,
106
+ 'title ' : 'The Title' ,
107
+ 'content ' : "<p>Foobar</p> " ,
108
+ 'start_date ' : "2018 -05-25 12 :05" ,
109
+ 'end_date ' : "2017-05-26 12:05" ,
110
+ 'notice_type ' : 'Foo' ,
111
+ 'notice_category' : 'Bar'
112
+ } )
76
113
context = {}
77
114
self .assertFalse (_save_notice (request , context ))
78
115
self .assertTrue (context ['date_error' ])
79
116
80
117
# no start
81
- notice_context = {
82
- 'action' : 'save' ,
83
- 'title ' : 'The Title ' ,
84
- 'content ' : "<p>Foobar</p>" ,
85
- 'end_date ' : "2017-05-26T12:05:00+00:00 " ,
86
- 'notice_type ' : 'Foo' ,
87
- 'notice_category ' : 'Bar'
88
- }
89
- request = rf . post ( '' , notice_context )
118
+ request = self . _get_request (
119
+ {
120
+ 'action ' : 'save ' ,
121
+ 'title ' : 'The Title' ,
122
+ 'content ' : "<p>Foobar</p> " ,
123
+ 'end_date ' : "2017-05-26 12:05" ,
124
+ 'notice_type ' : 'Foo' ,
125
+ 'notice_category' : 'Bar'
126
+ } )
90
127
context = {}
91
128
self .assertFalse (_save_notice (request , context ))
92
129
self .assertTrue (context ['start_error' ])
93
130
94
131
# Missing Attrs
95
- notice_context = {
96
- 'action' : 'save' ,
97
- }
98
- request = rf .post ('' , notice_context )
132
+ request = self ._get_request ({'action' : 'save' , })
99
133
context = {}
100
134
self .assertFalse (_save_notice (request , context ))
135
+ print (context )
101
136
self .assertTrue (context ['start_error' ])
102
137
self .assertTrue (context ['type_error' ])
103
138
self .assertTrue (context ['category_error' ])
104
139
self .assertTrue (context ['title_error' ])
105
140
self .assertTrue (context ['content_error' ])
106
141
107
- def test_html_content (self ):
108
- notice_context = {
109
- 'action' : 'save' ,
110
- 'title' : '<b>The</b> <p>Title</p>' ,
111
- 'content' : "<p>allowed tag</p> <script>not allowed</script>" ,
112
- 'start_date' : "2018-05-05T12:05:00+00:00" ,
113
- 'end_date' : "2018-05-26T12:05:00+00:00" ,
114
- 'notice_type' : 'Foo' ,
115
- 'notice_category' : 'Bar'
116
- }
117
- rf = RequestFactory ()
118
- request = rf .post ('' , notice_context )
142
+ def test_content_allowed_tags (self ):
143
+ request = self ._get_request (
144
+ {
145
+ 'action' : 'save' ,
146
+ 'title' : '<b>The</b> Title' ,
147
+ 'content' : "<p>allowed tag</p> <span>not allowed</span>" ,
148
+ 'start_date' : "2018-05-05 12:05" ,
149
+ 'end_date' : "2018-05-26 12:05" ,
150
+ 'notice_type' : 'Foo' ,
151
+ 'notice_category' : 'Bar'
152
+ })
119
153
_save_notice (request , {})
120
154
121
155
request = get_request_with_date ("2018-05-09" )
122
156
get_request_with_user ('javerage' , request )
123
157
notices = get_myuw_notices_for_user (request )
124
158
125
- self .assertEqual (notices [0 ].title , "<b>The</b> <p>"
126
- "Title</p>" )
159
+ self .assertEqual (notices [0 ].title , "<b>The</b> Title" )
127
160
128
- self .assertEqual (notices [0 ].content , "<p>allowed tag</p> <script"
129
- ">not allowed</script>" )
161
+ self .assertEqual (
162
+ notices [0 ].content ,
163
+ "<p>allowed tag</p> <span>not allowed</span>" )
130
164
131
- def test_edit_notice (self ):
165
+ def test_target_group (self ):
132
166
notice_context = {
133
167
'action' : 'save' ,
134
168
'title' : 'Test Edit' ,
135
169
'content' : "Foo" ,
136
- 'start_date' : "2013-03-27T13:00:00+00 :00" ,
137
- 'end_date' : "2013-05-06T23 :13:00+00:00 " ,
170
+ 'start_date' : "2013-03-27 13 :00" ,
171
+ 'end_date' : "2013-05-06 23 :13" ,
138
172
'notice_type' : 'Foo' ,
139
173
'notice_category' : 'Bar' ,
140
174
'target_group' : 'u_astratst_myuw_test-support-admin'
141
175
}
142
- rf = RequestFactory ()
143
- request = rf .post ('' , notice_context )
176
+ request = self ._get_request (notice_context )
144
177
self .assertTrue (_save_notice (request , {}))
145
178
146
179
notice_context ['action' ] = 'edit'
@@ -150,7 +183,7 @@ def test_edit_notice(self):
150
183
entries = MyuwNotice .objects .all ()
151
184
self .assertEqual (len (entries ), 1 )
152
185
153
- request = rf . post ( '' , notice_context )
186
+ request = self . _get_request ( notice_context )
154
187
self .assertTrue (_save_notice (request , {},
155
188
notice_id = entries [0 ].id ))
156
189
0 commit comments