1
- from myuw .test .api import MyuwApiTest
2
- from myuw .models import VisitedLink , PopularLink , CustomLink , HiddenLink
1
+ import json
3
2
from django .core .urlresolvers import reverse
4
3
from myuw .test import fdao_class_website_override
5
- import json
4
+ from myuw .test .api import MyuwApiTest
5
+ from myuw .models import VisitedLink , PopularLink , CustomLink , HiddenLink
6
+ from myuw .views .api .link import get_link_data
6
7
7
8
8
9
@fdao_class_website_override
9
10
class TestQuickLinksAPI (MyuwApiTest ):
11
+
12
+ def test_get_link_data (self ):
13
+ data = {'type' : 'custom' ,
14
+ 'url' : 'www.washington.edu/'
15
+ }
16
+ url , label = get_link_data (data , get_id = False )
17
+ self .assertEquals (url , 'http://www.washington.edu/' )
18
+ self .assertEquals (label , 'http://www.washington.edu/' )
19
+
20
+ data = {'type' : 'custom' ,
21
+ 'url' : 'www.washington.edu/' ,
22
+ 'label' : 'UW Homepage'
23
+ }
24
+ url , label = get_link_data (data , get_id = False )
25
+ self .assertEquals (label , 'UW Homepage' )
26
+
27
+ data = {'type' : 'custom' ,
28
+ 'url' : 'www.washington.edu/' ,
29
+ 'label' : 'UW Homepage' ,
30
+ 'id' : 1
31
+ }
32
+ link_id , url , label = get_link_data (data )
33
+ self .assertEquals (link_id , 1 )
34
+
10
35
def test_add_popular_link (self ):
11
36
PopularLink .objects .all ().delete ()
12
37
CustomLink .objects .all ().delete ()
@@ -109,9 +134,9 @@ def test_bad_syntax(self):
109
134
self .assertEquals (response .status_code , 404 )
110
135
111
136
def test_add_pure_custom (self ):
137
+ CustomLink .objects .all ().delete ()
112
138
self .set_user ('javerage' )
113
139
url = reverse ('myuw_manage_links' )
114
- CustomLink .objects .all ().delete ()
115
140
116
141
data = json .dumps ({'type' : 'custom' ,
117
142
'url' : 'www.washington.edu/classroom/SMI+401'
@@ -127,7 +152,7 @@ def test_add_pure_custom(self):
127
152
'http://www.washington.edu/classroom/SMI+401' )
128
153
self .assertEqual (all [0 ].label , 'Room Information' )
129
154
130
- # Same w/ protocol
155
+ # Add the same link but w/ protocol
131
156
data = json .dumps ({'type' : 'custom' ,
132
157
'url' : 'http://www.washington.edu/classroom/SMI+401'
133
158
})
@@ -138,7 +163,7 @@ def test_add_pure_custom(self):
138
163
all = CustomLink .objects .all ()
139
164
self .assertEqual (len (all ), 1 )
140
165
141
- # https is different though
166
+ # https is different
142
167
http_url = 'https://www.washington.edu/classroom/SMI+401'
143
168
data = json .dumps ({'type' : 'custom' ,
144
169
'url' : http_url
@@ -149,49 +174,44 @@ def test_add_pure_custom(self):
149
174
150
175
all = CustomLink .objects .all ()
151
176
self .assertEqual (len (all ), 2 )
177
+ self .assertEqual (all [0 ].url ,
178
+ 'http://www.washington.edu/classroom/SMI+401' )
179
+ self .assertEqual (all [1 ].url ,
180
+ 'https://www.washington.edu/classroom/SMI+401' )
152
181
153
- # Make sure we do a reasonable job w/ urls we can't resolve
154
- data = json .dumps ({'type' : 'custom' ,
155
- 'url' : 'http://www.washington.edu/classroom/404'
156
- })
182
+ # not http/https url
183
+ data = json .dumps ({
184
+ 'type' : 'custom' ,
185
+ 'url' : 'webcal://www.trumba.com/calendars/sea_acad-cal.ics'
186
+ })
157
187
158
188
response = self .client .post (url , data , content_type = 'application_json' )
159
- self .assertEqual (response .status_code , 404 )
189
+ self .assertEqual (response .status_code , 200 )
190
+ all = CustomLink .objects .all ()
191
+ self .assertEqual (len (all ), 3 )
160
192
161
193
def test_edit_custom_link (self ):
194
+ CustomLink .objects .all ().delete ()
162
195
self .set_user ('javerage' )
163
196
url = reverse ('myuw_manage_links' )
164
- CustomLink .objects .all ().delete ()
165
-
197
+ # add link
166
198
data = json .dumps ({'type' : 'custom' ,
167
199
'url' : 'www.washington.edu/classroom/SMI+401'
168
200
})
169
201
170
202
response = self .client .post (url , data , content_type = 'application_json' )
171
203
self .assertEqual (response .status_code , 200 )
172
-
173
- link_id = CustomLink .objects .all ()[0 ].pk
174
-
175
- # Try to edit the link as someone else
176
- self .set_user ('jpce' )
204
+ all = CustomLink .objects .all ()
205
+ self .assertEqual (len (all ), 1 )
206
+ # edit
207
+ link_id = all [0 ].pk
177
208
data = json .dumps ({'type' : 'custom-edit' ,
178
209
'url' : 'http://example.com' ,
179
210
'label' : 'Just example' ,
180
211
'id' : link_id ,
181
212
})
182
213
response = self .client .post (url , data , content_type = 'application_json' )
183
- self .assertEqual (response .status_code , 404 )
184
-
185
- all = CustomLink .objects .all ()
186
- self .assertEquals (len (all ), 1 )
187
- link = all [0 ]
188
- self .assertEquals (link .url ,
189
- 'http://www.washington.edu/classroom/SMI+401' )
190
-
191
- self .set_user ('javerage' )
192
- response = self .client .post (url , data , content_type = 'application_json' )
193
214
self .assertEqual (response .status_code , 200 )
194
-
195
215
all = CustomLink .objects .all ()
196
216
self .assertEquals (len (all ), 1 )
197
217
link = all [0 ]
@@ -200,7 +220,7 @@ def test_edit_custom_link(self):
200
220
201
221
# Make sure links actually have a label...
202
222
data = json .dumps ({'type' : 'custom-edit' ,
203
- 'url' : 'http://example.com ' ,
223
+ 'url' : 'www.washington.edu/classroom/SMI+401 ' ,
204
224
'label' : ' ' ,
205
225
'id' : link_id ,
206
226
})
@@ -215,11 +235,11 @@ def test_edit_custom_link(self):
215
235
self .assertEquals (link .label , 'Just example' )
216
236
217
237
def test_remove_link (self ):
238
+ CustomLink .objects .all ().delete ()
239
+
218
240
# Add a link as 2 users, make sure we can remove ours, but not theirs
219
241
self .set_user ('javerage' )
220
242
url = reverse ('myuw_manage_links' )
221
- CustomLink .objects .all ().delete ()
222
-
223
243
data = json .dumps ({'type' : 'custom' ,
224
244
'url' : 'www.washington.edu/classroom/SMI+401'
225
245
})
@@ -258,26 +278,23 @@ def test_remove_default_by_url(self):
258
278
self .set_user ('javerage' )
259
279
url = reverse ('myuw_manage_links' )
260
280
281
+ # add HiddenLink
261
282
data = json .dumps ({'type' : 'hide' ,
262
283
'id' : 'http://example.com' })
263
-
264
284
response = self .client .post (url , data , content_type = 'application_json' )
265
285
self .assertEquals (response .status_code , 200 )
266
286
all = HiddenLink .objects .all ()
267
-
268
287
self .assertEqual (len (all ), 1 )
269
288
self .assertEqual (all [0 ].url , 'http://example.com' )
270
-
289
+ # same link second time
271
290
response = self .client .post (url , data , content_type = 'application_json' )
272
291
self .assertEquals (response .status_code , 200 )
273
292
all = HiddenLink .objects .all ()
274
-
275
293
self .assertEqual (len (all ), 1 )
276
-
294
+ # Hide a non-default
277
295
data = json .dumps ({'type' : 'hide' ,
278
- 'id' : 'http://uw.edu' })
279
-
296
+ 'url' : 'http://uw.edu' })
280
297
response = self .client .post (url , data , content_type = 'application_json' )
281
- self .assertEquals (response .status_code , 200 )
298
+ self .assertEquals (response .status_code , 404 )
282
299
all = HiddenLink .objects .all ()
283
- self .assertEqual (len (all ), 2 )
300
+ self .assertEqual (len (all ), 1 )
0 commit comments