badgeapi 0.3.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +14 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +31 -0
  6. data/Rakefile +15 -0
  7. data/badgeapi.gemspec +33 -0
  8. data/lib/badgeapi/badge.rb +29 -0
  9. data/lib/badgeapi/badgeapi_object.rb +149 -0
  10. data/lib/badgeapi/collection.rb +10 -0
  11. data/lib/badgeapi/errors/api_error.rb +5 -0
  12. data/lib/badgeapi/errors/badgeapi_error.rb +18 -0
  13. data/lib/badgeapi/errors/invalid_request_error.rb +5 -0
  14. data/lib/badgeapi/recipient.rb +18 -0
  15. data/lib/badgeapi/version.rb +3 -0
  16. data/lib/badgeapi.rb +36 -0
  17. data/test/badge/badge_test.rb +469 -0
  18. data/test/badgeapi_test.rb +20 -0
  19. data/test/collection/collection_test.rb +275 -0
  20. data/test/fixtures/all_badges.yml +121 -0
  21. data/test/fixtures/all_badges_bad_user.yml +138 -0
  22. data/test/fixtures/all_badges_expanded.yml +196 -0
  23. data/test/fixtures/all_badges_from_collection.yml +52 -0
  24. data/test/fixtures/all_badges_issued.yml +430 -0
  25. data/test/fixtures/all_badges_limited.yml +49 -0
  26. data/test/fixtures/all_collection.yml +50 -0
  27. data/test/fixtures/all_collection_expanded.yml +131 -0
  28. data/test/fixtures/all_collection_limit.yml +49 -0
  29. data/test/fixtures/bad_Recipient.yml +183 -0
  30. data/test/fixtures/bad_Recipietn_request.yml +93 -0
  31. data/test/fixtures/bad_api_key.yml +49 -0
  32. data/test/fixtures/badge_error.yml +47 -0
  33. data/test/fixtures/badge_requirements.yml +351 -0
  34. data/test/fixtures/collection_error.yml +91 -0
  35. data/test/fixtures/create_badge.yml +97 -0
  36. data/test/fixtures/create_collection.yml +97 -0
  37. data/test/fixtures/create_new_badge_failure.yml +143 -0
  38. data/test/fixtures/create_new_collection_failure.yml +189 -0
  39. data/test/fixtures/destroy_badge.yml +277 -0
  40. data/test/fixtures/destroy_badge_error.yml +231 -0
  41. data/test/fixtures/destroy_collection.yml +185 -0
  42. data/test/fixtures/destroy_collection_error.yml +185 -0
  43. data/test/fixtures/issue_already_owned_badge.yml +49 -0
  44. data/test/fixtures/issue_badge_to_bad_user.yml +50 -0
  45. data/test/fixtures/issue_badge_to_user.yml +199 -0
  46. data/test/fixtures/issue_badge_to_user_with_library_card.yml +103 -0
  47. data/test/fixtures/one_badge.yml +49 -0
  48. data/test/fixtures/one_badge_expanded.yml +50 -0
  49. data/test/fixtures/one_collection.yml +49 -0
  50. data/test/fixtures/one_collection_expanded.yml +104 -0
  51. data/test/fixtures/recipient_with_badges.yml +187 -0
  52. data/test/fixtures/recipient_with_badges_unicard.yml +50 -0
  53. data/test/fixtures/revoke_badge_from_user.yml +199 -0
  54. data/test/fixtures/revoke_badge_not_issued.yml +49 -0
  55. data/test/fixtures/update_badge_via_update.yml +145 -0
  56. data/test/fixtures/update_badge_via_update_slug_history.yml +191 -0
  57. data/test/fixtures/update_collection.yml +191 -0
  58. data/test/fixtures/update_collection_via_update.yml +145 -0
  59. data/test/recipient/recipient_test.rb +108 -0
  60. data/test/test_helper.rb +11 -0
  61. metadata +274 -0
@@ -0,0 +1,469 @@
1
+ #test/badge/badge_test.rb
2
+ require './test/test_helper'
3
+
4
+ class BadgeapiBadgeTest < MiniTest::Test
5
+
6
+ def self.test_order
7
+ :alpha
8
+ end
9
+
10
+ def test_exists
11
+ assert Badgeapi::Badge
12
+ end
13
+
14
+ def test_object_path
15
+ assert_equal "badges", Badgeapi::Badge.collection_path
16
+ end
17
+
18
+ def test_object_name
19
+ assert_equal "badge", Badgeapi::Badge.member_name
20
+ end
21
+
22
+ def test_it_returns_back_a_single_badge
23
+ VCR.use_cassette('one_badge', :record => :all) do
24
+ Badgeapi.api_key = "c9cde524238644fa93393159e5e9ad87"
25
+
26
+ badge = Badgeapi::Badge.find(1)
27
+ assert_equal Badgeapi::Badge, badge.class
28
+
29
+ assert_equal "book-worm", badge.id
30
+ assert_equal "Book Worm", badge.name
31
+ assert_equal "You have loaned out over 25 books. Nice going!", badge.description
32
+ assert_equal "Loan out 25 books", badge.requirements
33
+ assert_equal "You must like books...", badge.hint
34
+ #assert_equal "http://openbadges.org/wp-content/themes/openbadges2/media/images/content-background.png", badge.image
35
+ assert_equal "library", badge.collection_id
36
+ assert_equal "bronze", badge.level
37
+ assert_equal 25, badge.points
38
+ assert_equal false, badge.auto_issue
39
+ assert_equal "badge", badge.object
40
+ assert_equal "live", badge.status
41
+ end
42
+ end
43
+
44
+ def test_it_returns_back_a_single_badge_expanded
45
+ VCR.use_cassette('one_badge_expanded', :record => :all) do
46
+ Badgeapi.api_key = "c9cde524238644fa93393159e5e9ad87"
47
+
48
+ badge = Badgeapi::Badge.find(1, expand: "collection")
49
+ assert_equal Badgeapi::Badge, badge.class
50
+
51
+ assert_equal "book-worm", badge.id
52
+ assert_equal "Book Worm", badge.name
53
+ assert_equal "You have loaned out over 25 books. Nice going!", badge.description
54
+ assert_equal "Loan out 25 books", badge.requirements
55
+ assert_equal "You must like books...", badge.hint
56
+ #assert_equal "http://openbadges.org/wp-content/themes/openbadges2/media/images/content-background.png", badge.image
57
+ assert_equal "library", badge.collection_id
58
+
59
+ assert_equal Badgeapi::Collection, badge.collection.class
60
+ assert_equal "Library", badge.collection.name
61
+ end
62
+ end
63
+
64
+ def test_it_returns_back_all_badges_issued_to_user
65
+ VCR.use_cassette('all_badges_issued', :record => :all) do
66
+ Badgeapi.api_key = "c9cde524238644fa93393159e5e9ad87"
67
+ result = Badgeapi::Badge.all(user: "t.skarbek-wazynski@lancaster.ac.uk")
68
+
69
+ # Make sure we got all the badges
70
+ assert_equal 1, result.length
71
+
72
+ # Make sure that the JSON was parsed
73
+ assert result.kind_of?(Array)
74
+ assert result.first.kind_of?(Badgeapi::Badge)
75
+
76
+ result = Badgeapi::Badge.all(user: "0043181") #uni card number for t.skarbek-wazynski
77
+
78
+ # Make sure we got all the badges
79
+ assert_equal 1, result.length
80
+
81
+ # Make sure that the JSON was parsed
82
+ assert result.kind_of?(Array)
83
+ assert result.first.kind_of?(Badgeapi::Badge)
84
+ end
85
+ end
86
+
87
+ def test_it_raises_error_for_bad_user
88
+ VCR.use_cassette('all_badges_bad_user', :record => :all) do
89
+ Badgeapi.api_key = "c9cde524238644fa93393159e5e9ad87"
90
+ assert_raises(Badgeapi::InvalidRequestError) { Badgeapi::Badge.all(user: "t.skarbek-wazynski") }
91
+ end
92
+ end
93
+
94
+ def test_it_raises_error_for_non_existing_user
95
+ VCR.use_cassette('all_badges_bad_user', :record => :all) do
96
+ Badgeapi.api_key = "c9cde524238644fa93393159e5e9ad87"
97
+ assert_raises(Badgeapi::InvalidRequestError) { Badgeapi::Badge.all(user: "t.skarbek-wazynsky@lancaster.ac.uk") }
98
+ assert_raises(Badgeapi::InvalidRequestError) { Badgeapi::Badge.all(user: "081897144451") }
99
+ end
100
+ end
101
+
102
+ def test_it_returns_back_all_badges
103
+ VCR.use_cassette('all_badges', :record => :all) do
104
+ Badgeapi.api_key = "c9cde524238644fa93393159e5e9ad87"
105
+ result = Badgeapi::Badge.all
106
+
107
+ # Make sure we got all the badges
108
+ assert_equal 6, result.length
109
+
110
+ # Make sure that the JSON was parsed
111
+ assert result.kind_of?(Array)
112
+ assert result.first.kind_of?(Badgeapi::Badge)
113
+ end
114
+ end
115
+
116
+ def test_it_returns_back_all_badges_exapanded
117
+ VCR.use_cassette('all_badges_expanded', :record => :all) do
118
+ Badgeapi.api_key = "c9cde524238644fa93393159e5e9ad87"
119
+ result = Badgeapi::Badge.all(expand: "collection")
120
+
121
+ # Make sure we got all the badges
122
+ assert_equal 6, result.length
123
+
124
+ # Make sure that the JSON was parsed
125
+ assert result.kind_of?(Array)
126
+ assert result.first.kind_of?(Badgeapi::Badge)
127
+ assert result.first.collection.kind_of?(Badgeapi::Collection)
128
+
129
+ #assert_not_nil result['collection']
130
+ end
131
+ end
132
+
133
+ def test_it_errors_all_badges_exapanded
134
+ VCR.use_cassette('all_badges_expanded', :record => :all) do
135
+ Badgeapi.api_key = "c9cde524238644fa93393159e5e9ad87"
136
+
137
+ assert_raises(Badgeapi::InvalidRequestError) { Badgeapi::Badge.all(expand: "monkey") }
138
+ end
139
+ end
140
+
141
+ def test_it_returns_back_all_badges_from_collection
142
+ VCR.use_cassette('all_badges_from_collection') do
143
+ Badgeapi.api_key = "c9cde524238644fa93393159e5e9ad87"
144
+ result = Badgeapi::Badge.all(collection_id: 2)
145
+
146
+ # Make sure we got all the badges
147
+ assert_equal 2, result.length
148
+
149
+ # Make sure that the JSON was parsed
150
+ assert result.kind_of?(Array)
151
+ assert result.first.kind_of?(Badgeapi::Badge)
152
+ end
153
+ end
154
+
155
+ def test_all_limit
156
+ VCR.use_cassette('all_badges_limited', :record => :all) do
157
+ Badgeapi.api_key = "c9cde524238644fa93393159e5e9ad87"
158
+ result = Badgeapi::Badge.all(limit: 1)
159
+
160
+ # Make sure we got all the badges
161
+ assert_equal 1, result.length
162
+
163
+ # Make sure that the JSON was parsed
164
+ assert result.kind_of?(Array)
165
+ assert result.first.kind_of?(Badgeapi::Badge)
166
+ end
167
+ end
168
+
169
+ def test_badges_raise_errors
170
+ VCR.use_cassette('badge_error', :record => :all) do
171
+ Badgeapi.api_key= 'c9cde524238644fa93393159e5e9ad87'
172
+ assert_raises(Badgeapi::InvalidRequestError) { Badgeapi::Badge.find(27) }
173
+ end
174
+ end
175
+
176
+ def test_create_a_new_badge
177
+ VCR.use_cassette('create_badge', :record => :all) do
178
+ Badgeapi.api_key = 'c9cde524238644fa93393159e5e9ad87'
179
+
180
+ badge = Badgeapi::Badge.create(
181
+ name: "Create Badge Test",
182
+ description: "This is a new badge",
183
+ requirements: "You need to love the Badge API",
184
+ hint: "Love us..",
185
+ collection_id: 1,
186
+ level: "silver",
187
+ auto_issue: true
188
+ )
189
+
190
+ assert_equal Badgeapi::Badge, badge.class
191
+ assert_equal "Create Badge Test", badge.name
192
+ assert_equal "This is a new badge", badge.description
193
+ assert_equal "You need to love the Badge API", badge.requirements
194
+ assert_equal "Love us..", badge.hint
195
+ #assert_equal "http://example.org/badge.png", badge.image
196
+ assert_equal "library", badge.collection_id
197
+ assert_equal true, badge.auto_issue
198
+
199
+ Badgeapi::Badge.destroy(badge.id)
200
+ end
201
+ end
202
+
203
+ def test_create_new_badge_failure
204
+ VCR.use_cassette('create_new_badge_failure', :record => :all) do
205
+ Badgeapi.api_key = 'c9cde524238644fa93393159e5e9ad87'
206
+
207
+ badge = Badgeapi::Badge.create(
208
+ name: "Create Badge Test Destroy",
209
+ description: "This is a new badge",
210
+ requirements: "You need to love the Badge API",
211
+ hint: "Love us..",
212
+ collection_id: 1,
213
+ level: "bronze"
214
+ )
215
+
216
+ assert_raises(Badgeapi::InvalidRequestError) {
217
+ Badgeapi::Badge.create(
218
+ name: "Create Badge Test Destroy",
219
+ description: "This is a new badge",
220
+ requirements: "You need to love the Badge API",
221
+ hint: "Love us..",
222
+ collection_id: 1,
223
+ level: "bronze"
224
+ )
225
+ }
226
+
227
+ Badgeapi::Badge.destroy(badge.id)
228
+ end
229
+ end
230
+
231
+ def test_badge_destroy
232
+ VCR.use_cassette('destroy_badge', :record => :all) do
233
+ Badgeapi.api_key= 'c9cde524238644fa93393159e5e9ad87'
234
+
235
+ badge = Badgeapi::Badge.create(
236
+ name: "Create Badge for Destroy",
237
+ description: "This is a new badge",
238
+ requirements: "You need to love the Badge API",
239
+ hint: "Love us..",
240
+ collection_id: 1,
241
+ level: "bronze"
242
+ )
243
+
244
+ destroyed_badge = Badgeapi::Badge.destroy(badge.id)
245
+
246
+ assert_equal Badgeapi::Badge, destroyed_badge.class
247
+
248
+ assert_raises(Badgeapi::InvalidRequestError) { Badgeapi::Badge.destroy(badge.id) }
249
+
250
+ end
251
+ end
252
+
253
+ def test_badge_destroy_error
254
+ VCR.use_cassette('destroy_badge_error', :record => :all) do
255
+ Badgeapi.api_key= 'c9cde524238644fa93393159e5e9ad87'
256
+
257
+ badge = Badgeapi::Badge.create(
258
+ name: "Create Badge for Destroy",
259
+ description: "This is a new badge",
260
+ requirements: "You need to love the Badge API",
261
+ hint: "Love us..",
262
+ collection_id: 1,
263
+ level: "bronze"
264
+ )
265
+
266
+ destroyed_badge = Badgeapi::Badge.destroy(badge.id)
267
+
268
+ assert_raises(Badgeapi::InvalidRequestError) { Badgeapi::Badge.destroy(badge.id) }
269
+ end
270
+ end
271
+
272
+ def test_update_badge_via_update
273
+ VCR.use_cassette('update_badge_via_update', :record => :all) do
274
+ Badgeapi.api_key= 'c9cde524238644fa93393159e5e9ad87'
275
+
276
+ badge = Badgeapi::Badge.create(
277
+ name: "Create Badge for update",
278
+ description: "This is a new badge",
279
+ requirements: "You need to love the Badge API",
280
+ hint: "Love us..",
281
+ collection_id: 1,
282
+ level: "bronze"
283
+ )
284
+
285
+ updated_badge = Badgeapi::Badge.update(badge.id,
286
+ name: "Updated Badge",
287
+ description: "Updated Description",
288
+ requirements: "Updated Requirements",
289
+ hint: "Updated Hint",
290
+ collection_id: "trim-trail",
291
+ status: "live",
292
+ )
293
+
294
+ assert_equal "Updated Badge", updated_badge.name
295
+ assert_equal "Updated Description", updated_badge.description
296
+ assert_equal "Updated Requirements", updated_badge.requirements
297
+ assert_equal "Updated Hint", updated_badge.hint
298
+ assert_equal "trim-trail", updated_badge.collection_id
299
+ assert_equal "live", updated_badge.status
300
+
301
+ Badgeapi::Badge.destroy(badge.id)
302
+ end
303
+ end
304
+
305
+ def test_update_badge_via_update_with_slug_update_and_history
306
+ VCR.use_cassette('update_badge_via_update_slug_history', :record => :all) do
307
+ Badgeapi.api_key= 'c9cde524238644fa93393159e5e9ad87'
308
+
309
+ badge = Badgeapi::Badge.create(
310
+ name: "Create Badge for update",
311
+ description: "This is a new badge",
312
+ requirements: "You need to love the Badge API",
313
+ hint: "Love us..",
314
+ collection_id: 1,
315
+ level: "bronze"
316
+ )
317
+
318
+ assert_equal "create-badge-for-update", badge.id
319
+
320
+
321
+ updated_badge = Badgeapi::Badge.update(badge.id,
322
+ name: "Updated Badge",
323
+ description: "Updated Description",
324
+ requirements: "Updated Requirements",
325
+ hint: "Updated Hint",
326
+ collection_id: "trim-trail"
327
+ )
328
+
329
+ assert_equal "updated-badge", updated_badge.id
330
+ assert_equal "Updated Badge", updated_badge.name
331
+ assert_equal "Updated Description", updated_badge.description
332
+ assert_equal "Updated Requirements", updated_badge.requirements
333
+ assert_equal "Updated Hint", updated_badge.hint
334
+ assert_equal "trim-trail", updated_badge.collection_id
335
+
336
+ using_old_slug = Badgeapi::Badge.find(badge.id)
337
+
338
+ assert_equal "Updated Badge", using_old_slug.name
339
+
340
+ Badgeapi::Badge.destroy(badge.id)
341
+ end
342
+ end
343
+
344
+ def test_should_issue_badge_with_email
345
+ VCR.use_cassette('issue_badge_to_user', :record => :all) do
346
+ Badgeapi.api_key= 'c9cde524238644fa93393159e5e9ad87'
347
+
348
+ recipient = Badgeapi::Badge.issue(
349
+ "mega-book-worm",
350
+ recipient: "t.skarbek-wazynski@lancaster.ac.uk"
351
+ )
352
+
353
+ assert_equal 2, recipient.badges.length
354
+ assert recipient.kind_of?(Badgeapi::Recipient)
355
+ assert recipient.badges.first.kind_of?(Badgeapi::Badge)
356
+
357
+ recipient = Badgeapi::Badge.revoke(
358
+ "mega-book-worm",
359
+ recipient: "t.skarbek-wazynski@lancaster.ac.uk"
360
+ )
361
+
362
+ assert_equal 1, recipient.badges.length
363
+ end
364
+ end
365
+
366
+ def test_should_revoke_badge_with_email
367
+ VCR.use_cassette('revoke_badge_from_user', :record => :all) do
368
+ Badgeapi.api_key= 'c9cde524238644fa93393159e5e9ad87'
369
+
370
+ recipient = Badgeapi::Badge.issue(
371
+ "mega-book-worm",
372
+ recipient: "t.skarbek-wazynski@lancaster.ac.uk"
373
+ )
374
+
375
+ assert_equal 2, recipient.badges.length
376
+
377
+ recipient = Badgeapi::Badge.revoke(
378
+ "mega-book-worm",
379
+ recipient: "t.skarbek-wazynski@lancaster.ac.uk"
380
+ )
381
+
382
+ assert_equal 1, recipient.badges.length
383
+ assert recipient.kind_of?(Badgeapi::Recipient)
384
+ assert recipient.badges.first.kind_of?(Badgeapi::Badge)
385
+ end
386
+ end
387
+
388
+ def test_should_issue_badge_with_library_card
389
+ VCR.use_cassette('issue_badge_to_user_with_library_card', :record => :all) do
390
+ Badgeapi.api_key= 'c9cde524238644fa93393159e5e9ad87'
391
+
392
+ recipient = Badgeapi::Badge.issue(
393
+ 3,
394
+ recipient: "0043181"
395
+ )
396
+
397
+
398
+ assert_equal 2, recipient.badges.length
399
+ assert recipient.kind_of?(Badgeapi::Recipient)
400
+ assert recipient.badges.first.kind_of?(Badgeapi::Badge)
401
+
402
+ recipient = Badgeapi::Badge.revoke(
403
+ 3,
404
+ recipient: "0043181"
405
+ )
406
+
407
+ assert_equal 1, recipient.badges.length
408
+ end
409
+ end
410
+
411
+ def test_should_error_when_user_param_is_bad
412
+ VCR.use_cassette('issue_badge_to_bad_user', :record => :all) do
413
+ Badgeapi.api_key= 'c9cde524238644fa93393159e5e9ad87'
414
+
415
+ assert_raises(Badgeapi::InvalidRequestError) {
416
+ Badgeapi::Badge.issue(
417
+ 2,
418
+ recipient: "t.skarbek-wazynski"
419
+ )
420
+ }
421
+ end
422
+ end
423
+
424
+ def test_should_error_when_issuing_bad_already_issued
425
+ VCR.use_cassette('issue_already_owned_badge', :record => :all) do
426
+ Badgeapi.api_key= 'c9cde524238644fa93393159e5e9ad87'
427
+
428
+ assert_raises(Badgeapi::InvalidRequestError) {
429
+ Badgeapi::Badge.issue(
430
+ 1,
431
+ recipient: "t.skarbek-wazynski@lancaster.ac.uk"
432
+ )
433
+ }
434
+ end
435
+ end
436
+
437
+ def test_should_error_when_revoking_badge_not_owned
438
+ VCR.use_cassette('revoke_badge_not_issued', :record => :all) do
439
+ Badgeapi.api_key= 'c9cde524238644fa93393159e5e9ad87'
440
+
441
+ assert_raises(Badgeapi::InvalidRequestError) {
442
+ Badgeapi::Badge.revoke(
443
+ 3,
444
+ recipient: "t.skarbek-wazynski@lancaster.ac.uk"
445
+ )
446
+ }
447
+ end
448
+ end
449
+
450
+ def test_badge_requirements
451
+ VCR.use_cassette('badge_requirements', :record => :all) do
452
+ Badgeapi.api_key= 'c9cde524238644fa93393159e5e9ad87'
453
+
454
+ badge = Badgeapi::Badge.find("marathon-man")
455
+
456
+ required_count = badge.required_badges.count
457
+
458
+ badge = Badgeapi::Badge.add_badge_requirement("marathon-man", "you-trim-trailed")
459
+
460
+ assert_equal required_count + 1, badge.required_badges.count
461
+ assert_equal Badgeapi::Badge, badge.required_badges.first.class
462
+
463
+ badge = Badgeapi::Badge.remove_badge_requirement("marathon-man", "you-trim-trailed")
464
+
465
+ assert_equal required_count, badge.required_badges.count
466
+ end
467
+ end
468
+
469
+ end
@@ -0,0 +1,20 @@
1
+ #test/badge/badge_test.rb
2
+ require './test/test_helper'
3
+
4
+ class BadgeapiTest < MiniTest::Test
5
+
6
+ def test_api_url
7
+ assert_equal Badgeapi.api_url, 'https://gamification-api.dev/v1'
8
+ end
9
+
10
+ def test_api_key
11
+ Badgeapi.api_key = 'foo'
12
+ assert_equal Badgeapi.api_key, 'foo'
13
+ end
14
+
15
+ def test_ssl_ca_cert
16
+ Badgeapi.ssl_ca_cert = 'foo'
17
+ assert_equal Badgeapi.ssl_ca_cert, 'foo'
18
+ end
19
+
20
+ end