mollom_rest_api 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +22 -0
  3. data/.travis.yml +4 -0
  4. data/Gemfile +18 -0
  5. data/Guardfile +4 -0
  6. data/LICENSE +21 -0
  7. data/README.md +86 -0
  8. data/Rakefile +6 -0
  9. data/lib/mollom_rest_api/exceptions.rb +12 -0
  10. data/lib/mollom_rest_api/interface.rb +81 -0
  11. data/lib/mollom_rest_api/v1/blacklist.rb +29 -0
  12. data/lib/mollom_rest_api/v1/captcha.rb +11 -0
  13. data/lib/mollom_rest_api/v1/content.rb +12 -0
  14. data/lib/mollom_rest_api/v1/feedback.rb +14 -0
  15. data/lib/mollom_rest_api/v1/site.rb +23 -0
  16. data/lib/mollom_rest_api/v1/whitelist.rb +29 -0
  17. data/lib/mollom_rest_api/v1.rb +3 -0
  18. data/lib/mollom_rest_api/version.rb +3 -0
  19. data/lib/mollom_rest_api/versioned_api.rb +51 -0
  20. data/lib/mollom_rest_api.rb +31 -0
  21. data/log/.gitkeep +0 -0
  22. data/mollom_rest_api.gemspec +21 -0
  23. data/spec/fixtures/cassette_library/blacklist/create.yml +43 -0
  24. data/spec/fixtures/cassette_library/blacklist/delete.yml +43 -0
  25. data/spec/fixtures/cassette_library/blacklist/list.yml +39 -0
  26. data/spec/fixtures/cassette_library/blacklist/read.yml +41 -0
  27. data/spec/fixtures/cassette_library/blacklist/update.yml +43 -0
  28. data/spec/fixtures/cassette_library/captcha/create/default_request.yml +45 -0
  29. data/spec/fixtures/cassette_library/captcha/create/invalid_type.yml +45 -0
  30. data/spec/fixtures/cassette_library/captcha/create/valid_request.yml +45 -0
  31. data/spec/fixtures/cassette_library/captcha/verify/invalid_captcha_id.yml +45 -0
  32. data/spec/fixtures/cassette_library/captcha/verify/invalid_solution.yml +46 -0
  33. data/spec/fixtures/cassette_library/captcha/verify/valid_solution.yml +46 -0
  34. data/spec/fixtures/cassette_library/content/create.yml +46 -0
  35. data/spec/fixtures/cassette_library/content/update/invalid_content_id.yml +45 -0
  36. data/spec/fixtures/cassette_library/content/update/valid_request.yml +45 -0
  37. data/spec/fixtures/cassette_library/feedback/add/captcha_request.yml +45 -0
  38. data/spec/fixtures/cassette_library/feedback/add/content_request.yml +45 -0
  39. data/spec/fixtures/cassette_library/interface/some_api/a_post_action.yml +32 -0
  40. data/spec/fixtures/cassette_library/interface/some_api/an_overriden_get_action.yml +26 -0
  41. data/spec/fixtures/cassette_library/interface/some_api/an_overriden_post_action.yml +32 -0
  42. data/spec/fixtures/cassette_library/interface/some_api/delete.yml +26 -0
  43. data/spec/fixtures/cassette_library/interface/some_api/list.yml +26 -0
  44. data/spec/fixtures/cassette_library/invalid_request.yml +59 -0
  45. data/spec/fixtures/cassette_library/site/create.yml +45 -0
  46. data/spec/fixtures/cassette_library/site/delete.yml +45 -0
  47. data/spec/fixtures/cassette_library/site/list.yml +75 -0
  48. data/spec/fixtures/cassette_library/site/read.yml +41 -0
  49. data/spec/fixtures/cassette_library/site/update.yml +43 -0
  50. data/spec/fixtures/cassette_library/whitelist/create.yml +43 -0
  51. data/spec/fixtures/cassette_library/whitelist/delete.yml +43 -0
  52. data/spec/fixtures/cassette_library/whitelist/list.yml +41 -0
  53. data/spec/fixtures/cassette_library/whitelist/read.yml +39 -0
  54. data/spec/fixtures/cassette_library/whitelist/update.yml +43 -0
  55. data/spec/fixtures/v_test_01.rb +27 -0
  56. data/spec/mollom_rest_api/exceptions_spec.rb +25 -0
  57. data/spec/mollom_rest_api/interface_spec.rb +43 -0
  58. data/spec/mollom_rest_api/v1/blacklist_spec.rb +64 -0
  59. data/spec/mollom_rest_api/v1/captcha_spec.rb +63 -0
  60. data/spec/mollom_rest_api/v1/content_spec.rb +41 -0
  61. data/spec/mollom_rest_api/v1/feedback_spec.rb +37 -0
  62. data/spec/mollom_rest_api/v1/site_spec.rb +63 -0
  63. data/spec/mollom_rest_api/v1/whitelist_spec.rb +64 -0
  64. data/spec/mollom_rest_api/v1_spec.rb +9 -0
  65. data/spec/mollom_rest_api/version_spec.rb +5 -0
  66. data/spec/mollom_rest_api/versioned_api_spec.rb +26 -0
  67. data/spec/mollom_rest_api_spec.rb +30 -0
  68. data/spec/spec_helper.rb +15 -0
  69. data/spec/support/macros/alternate_syntax_macro.rb +38 -0
  70. data/spec/support/shared_examples/api_error_handling.rb +33 -0
  71. data/spec/support/vcr.rb +37 -0
  72. metadata +192 -0
@@ -0,0 +1,64 @@
1
+ require "spec_helper"
2
+
3
+ describe MollomRestApi::V1::Blacklist do
4
+ describe :create do
5
+ context "when providing a valid url and email", vcr: {cassette_name: "blacklist/create"} do
6
+ let(:request_parameters) { {context: 'allFields'} }
7
+ let(:response) {{"context"=>"allFields", "created"=>"1409677772829", "id"=>"41a0e033-3d6a-465b-87d5-1ef60476da62", "match"=>"contains", "note"=>"", "reason"=>"unwanted", "status"=>"1", "value"=>"text"}}
8
+
9
+ it "should return a json response containing the blacklist entry" do
10
+ expect(MollomRestApi::V1::Blacklist.create('1lgj17lzuezlu1bn9ry4k3qz4k8nr42n', 'text', request_parameters)).to eq(response)
11
+ end
12
+ end
13
+
14
+ include_examples "api error handling", class_under_test: MollomRestApi::V1::Blacklist, method_under_test: :create, method_args: ['ABC', 'DEF']
15
+ end
16
+
17
+ describe :update do
18
+ context "when providing a public key and blacklist entry id", vcr: {cassette_name: "blacklist/update"} do
19
+ let(:request_parameters) { {value: 'patate', reason: 'profanity'} }
20
+ let(:response) {{"context"=>"allFields", "created"=>"1409677772829", "id"=>"41a0e033-3d6a-465b-87d5-1ef60476da62", "match"=>"contains", "note"=>"", "reason"=>"profanity", "status"=>"1", "value"=>"patate"}}
21
+
22
+ it "should return a json response containing the updated blacklist entry" do
23
+ expect(MollomRestApi::V1::Blacklist.update('1lgj17lzuezlu1bn9ry4k3qz4k8nr42n', '42a51362-bab6-4256-8e28-60d5df568250', request_parameters)).to eq(response)
24
+ end
25
+ end
26
+
27
+ include_examples "api error handling", class_under_test: MollomRestApi::V1::Blacklist, method_under_test: :update, method_args: %w(public_key blacklist_entry_id)
28
+ end
29
+
30
+ describe :delete do
31
+ context "when providing a public key and blacklist entry id", vcr: {cassette_name: "blacklist/delete"} do
32
+ it "should return a json response containing the deleted blacklist entry" do
33
+ expect{MollomRestApi::V1::Blacklist.delete('1lgj17lzuezlu1bn9ry4k3qz4k8nr42n', '42a51362-bab6-4256-8e28-60d5df568250')}.not_to raise_error
34
+ end
35
+ end
36
+
37
+ include_examples "api error handling", class_under_test: MollomRestApi::V1::Blacklist, method_under_test: :delete, method_args: %w(public_key blacklist_entry_id)
38
+ end
39
+
40
+ describe :list do
41
+ context "when providing a public key", vcr: {cassette_name: "blacklist/list"} do
42
+ let(:request_parameters) {{count: 5}}
43
+ let(:response) {{"entry"=>[{"context"=>"allFields", "created"=>"1410369319158", "id"=>"eafc878d-94f7-462d-bb67-49d5170a9a71", "match"=>"contains", "note"=>"", "reason"=>"unwanted", "status"=>"1", "value"=>"patate-chaude"}, {"context"=>"allFields", "created"=>"1410369319342", "id"=>"975805c8-ee06-4750-8cda-ec5626712c50", "match"=>"contains", "note"=>"", "reason"=>"unwanted", "status"=>"1", "value"=>"system_32"}]}}
44
+
45
+ it "should return a json response containing the list of blacklisted entries" do
46
+ expect(MollomRestApi::V1::Blacklist.list('1lgj17lzuezlu1bn9ry4k3qz4k8nr42n', request_parameters)).to eq(response)
47
+ end
48
+ end
49
+
50
+ include_examples "api error handling", class_under_test: MollomRestApi::V1::Blacklist, method_under_test: :list, method_args: %w(public_key)
51
+ end
52
+
53
+ describe :read do
54
+ context "when providing a public key and blacklist entry id", vcr: {cassette_name: "blacklist/read"} do
55
+ let(:response) {{"context"=>"allFields", "created"=>"1410452568617", "id"=>"68172853-2a81-4899-b09a-ff1f1bad88aa", "match"=>"contains", "note"=>"", "reason"=>"unwanted", "status"=>"1", "value"=>"text"}}
56
+
57
+ it "should return a json response containing the blacklist entry" do
58
+ expect(MollomRestApi::V1::Blacklist.read('1lgj17lzuezlu1bn9ry4k3qz4k8nr42n', '68172853-2a81-4899-b09a-ff1f1bad88aa')).to eq(response)
59
+ end
60
+ end
61
+
62
+ include_examples "api error handling", class_under_test: MollomRestApi::V1::Blacklist, method_under_test: :read, method_args: %w(public_key blacklist_entry_id)
63
+ end
64
+ end
@@ -0,0 +1,63 @@
1
+ require "spec_helper"
2
+
3
+ describe MollomRestApi::V1::Captcha do
4
+ describe :create do
5
+ context "when no type is passed", vcr: {cassette_name: "captcha/create/default_request"} do
6
+ let(:response) {{"id"=>"TEST1wquvxygw6nkom", "url"=>"http://dev.mollom.com/v1/captcha/TEST1wquvxygw6nkom.png"}}
7
+
8
+ it "should return the json response for an image captcha" do
9
+ expect(MollomRestApi::V1::Captcha.create).to eq(response)
10
+ end
11
+ end
12
+
13
+ context "when an invalid type is passed", vcr: {cassette_name: "captcha/create/invalid_type"} do
14
+ let(:response) {{"id"=>"TEST1ddgajo9ldvw11", "url"=>"http://dev.mollom.com/v1/captcha/TEST1ddgajo9ldvw11.mp3"}}
15
+
16
+ it "should return the json response for a mp3 captcha" do
17
+ expect(MollomRestApi::V1::Captcha.create("foobar")).to eq(response)
18
+ end
19
+ end
20
+
21
+ context "when an valid type is passed", vcr: {cassette_name: "captcha/create/valid_request"} do
22
+ let(:response) {{"id"=>"TEST190gkt2utz449q", "url"=>"https://dev.mollom.com/v1/captcha/TEST190gkt2utz449q.mp3"}}
23
+
24
+ it "should return the json response for that type of captcha" do
25
+ expect(MollomRestApi::V1::Captcha.create("audio", ssl: 1)).to eq(response)
26
+ end
27
+ end
28
+
29
+ include_examples "api error handling", class_under_test: MollomRestApi::V1::Captcha, method_under_test: :create
30
+ end
31
+
32
+ describe :verify do
33
+ context "when an invalid captcha id is passed", vcr: {cassette_name: "captcha/verify/invalid_captcha_id"} do
34
+ it "should raise an exception" do
35
+ expect{MollomRestApi::V1::Captcha.verify('inexistent_id', 'solution')}.to raise_exception do |exception|
36
+ expect(exception).to be_a(MollomRestApi::ApiException)
37
+ expect(exception.message).to eq("{\"code\":\"404\",\"message\":\"Captcha not found.\"}")
38
+ expect(exception.error_code).to eq(404)
39
+ end
40
+ end
41
+ end
42
+
43
+ context "when an valid captcha id is passed" do
44
+ context "when an invalid solution is passed", vcr: {cassette_name: "captcha/verify/invalid_solution"} do
45
+ let(:response) {{"id"=>"TEST15yj59df1pwv01", "solved"=>"0", "reason"=>"invalid_solution is not the correct solution."}}
46
+
47
+ it "should return the json response with an invalid captcha solution" do
48
+ expect(MollomRestApi::V1::Captcha.verify("TEST15yj59df1pwv01", "invalid_solution")).to eq(response)
49
+ end
50
+ end
51
+
52
+ context "when a valid solution is passed", vcr: {cassette_name: "captcha/verify/valid_solution"} do
53
+ let(:response) {{"id"=>"TEST1qravn2ikcz0k1", "solved"=>"1", "reason"=>"Correct response to image captcha."}}
54
+
55
+ it "should return the json response with a valid captcha solution" do
56
+ expect(MollomRestApi::V1::Captcha.verify("TEST1qravn2ikcz0k1", "correct")).to eq(response)
57
+ end
58
+ end
59
+ end
60
+
61
+ include_examples "api error handling", class_under_test: MollomRestApi::V1::Captcha, method_under_test: :verify, method_args: [9000, 'a_solution']
62
+ end
63
+ end
@@ -0,0 +1,41 @@
1
+ require "spec_helper"
2
+
3
+ describe MollomRestApi::V1::Content do
4
+ [:create, :check].each do |action|
5
+ describe action do
6
+ context "when the api responded correctly", vcr: {cassette_name: "content/create"} do
7
+ let(:request_parameters) {{postTitle: "My Title", postBody: "Some text right over here!", authorName: "Jean-Luc Picard"}}
8
+ let(:response) {{"id"=>"TEST1up51ut7qyu2b1", "spamScore"=>"0.5", "reason"=>"some secret reason", "postTitle"=>"My Title", "postBody"=>"Some text right over here!", "spamClassification"=>"unsure"}}
9
+
10
+ it "should return a json response classifying the content" do
11
+ expect(MollomRestApi::V1::Content.send(action, request_parameters)).to eq(response)
12
+ end
13
+ end
14
+
15
+ include_examples "api error handling", class_under_test: MollomRestApi::V1::Content, method_under_test: action
16
+ end
17
+ end
18
+
19
+ describe :update do
20
+ context "when the content id doesn't exist", vcr: {cassette_name: "content/update/invalid_content_id"} do
21
+ it "should raise an exception" do
22
+ expect{MollomRestApi::V1::Content.update('inexistent_content_id')}.to raise_exception do |exception|
23
+ expect(exception).to be_a(MollomRestApi::ApiException)
24
+ expect(exception.message).to eq("{\"code\":\"404\",\"message\":\"Content not found.\"}")
25
+ expect(exception.error_code).to eq(404)
26
+ end
27
+ end
28
+ end
29
+
30
+ context "when the content id does exist", vcr: {cassette_name: "content/update/valid_request"} do
31
+ let(:request_parameters) {{checks: "spam", postBody: "My modified body!"}}
32
+ let(:response) {{"id"=>"TESTmh3q39o9ktwj13", "spamScore"=>"0.5", "reason"=>"some secret reason", "postBody"=>"My modified body!", "spamClassification"=>"unsure"}}
33
+
34
+ it "should return a json response classifying the updated content" do
35
+ expect(MollomRestApi::V1::Content.update("TESTmh3q39o9ktwj13", request_parameters)).to eq(response)
36
+ end
37
+ end
38
+
39
+ include_examples "api error handling", class_under_test: MollomRestApi::V1::Content, method_under_test: :update, method_args: ["some_id"]
40
+ end
41
+ end
@@ -0,0 +1,37 @@
1
+ require "spec_helper"
2
+
3
+ describe MollomRestApi::V1::Feedback do
4
+ describe :create do
5
+ context "when no contentId or captchaId is specified" do
6
+ it "should raise an argument error" do
7
+ expect{MollomRestApi::V1::Feedback.create("spam")}.to raise_exception do |exception|
8
+ expect(exception).to be_a(ArgumentError)
9
+ expect(exception.message).to eq("Specify one of two parameters: contentId or captchaId.")
10
+ end
11
+ end
12
+ end
13
+
14
+ context "when any contentId is specified (valid or invalid)", vcr: {cassette_name: "feedback/add/content_request"} do
15
+ it "should have silently passed" do
16
+ expect(MollomRestApi::V1::Feedback.create("spam", contentId: "some_id")).to be_nil
17
+ end
18
+ end
19
+
20
+ context "when any captchaId is specified (valid or invalid)", vcr: {cassette_name: "feedback/add/captcha_request"} do
21
+ it "should have silently passed" do
22
+ expect(MollomRestApi::V1::Feedback.create("spam", captchaId: "some_id")).to be_nil
23
+ end
24
+ end
25
+
26
+ context "when a contentId and a captchaId are passed" do
27
+ it "should raise an argument error" do
28
+ expect{MollomRestApi::V1::Feedback.create("spam", captchaId: "one", contentId: "two")}.to raise_exception do |exception|
29
+ expect(exception).to be_a(ArgumentError)
30
+ expect(exception.message).to eq("Specify one of two parameters: contentId or captchaId.")
31
+ end
32
+ end
33
+ end
34
+
35
+ include_examples "api error handling", class_under_test: MollomRestApi::V1::Feedback, method_under_test: :create, method_args: ['a_reason', {contentId: 'one'}]
36
+ end
37
+ end
@@ -0,0 +1,63 @@
1
+ require "spec_helper"
2
+
3
+ describe MollomRestApi::V1::Site do
4
+ describe :create do
5
+ context "when providing a valid url and email", vcr: {cassette_name: "site/create"} do
6
+ let(:request_parameters) { {platformName: "Drupal", platformVersion: "6.20"} }
7
+ let(:response) { {"publicKey" => "44wvtjchwxaj1h90cqi1hxscwcpceylx", "privateKey" => "ton7cjeu271j15l94ip777uvv1pcgua3", "url" => "http://url", "email" => "an-email@gmail.com", "platformName" => "Drupal", "platformVersion" => "6.20", "expectedLanguages" => nil} }
8
+
9
+ it "should return a json response classifying the content" do
10
+ expect(MollomRestApi::V1::Site.create("http://url", "an-email@gmail.com", request_parameters)).to eq(response)
11
+ end
12
+ end
13
+
14
+ include_examples "api error handling", class_under_test: MollomRestApi::V1::Site, method_under_test: :create, method_args: ['http://url', 'an-email-address@gmail.com']
15
+ end
16
+
17
+ describe :update do
18
+ context 'when providing a valid public key', vcr: {cassette_name: 'site/update'} do
19
+ let(:request_parameters) { {platformName: "Drupal", platformVersion: "6.20"} }
20
+ let(:response) { {"publicKey"=>"1mkno2c8bsepo12ewu7v0mmpxro77o3u", "privateKey"=>"z2urchz11o21b9m76l0x51iz1ize75g0", "expectedLanguages"=>nil} }
21
+
22
+ it 'should return the updated site ' do
23
+ expect(MollomRestApi::V1::Site.update('1mkno2c8bsepo12ewu7v0mmpxro77o3u')).to eq(response)
24
+ end
25
+ end
26
+
27
+ include_examples "api error handling", class_under_test: MollomRestApi::V1::Site, method_under_test: :update, method_args: ['a_public_key']
28
+ end
29
+
30
+ describe :list do
31
+ context 'when listing the first 5 sites', vcr: {cassette_name: 'site/list'} do
32
+ let(:request_parameters) { {count: 5} }
33
+ let(:response) { {"site"=>[{"publicKey"=>"1cu4vdx7a6m9c1jc2425k1o9r8me2mrz", "privateKey"=>"1khj0vgpcqtjfwbbl5fr3wiykmr2ajro", "url"=>"http://url", "email"=>"an-email@gmail.com", "platformName"=>"Drupal", "platformVersion"=>"6.20", "expectedLanguages"=>nil}, {"publicKey"=>"geu7chlny71m18ve7inn41kuap9ztyja", "privateKey"=>"16o5626oykcsophjt31p1lim986rm5cq", "url"=>"http://url", "email"=>"an-email@gmail.com", "platformName"=>"Drupal", "platformVersion"=>"6.20", "expectedLanguages"=>nil}, {"publicKey"=>"44wvtjchwxaj1h90cqi1hxscwcpceylx", "privateKey"=>"ton7cjeu271j15l94ip777uvv1pcgua3", "url"=>"http://url", "email"=>"an-email@gmail.com", "platformName"=>"Drupal", "platformVersion"=>"6.20", "expectedLanguages"=>nil}]} }
34
+ it 'should return the list of sites' do
35
+ expect(MollomRestApi::V1::Site.list(request_parameters)).to eq(response)
36
+ end
37
+ end
38
+
39
+ include_examples "api error handling", class_under_test: MollomRestApi::V1::Site, method_under_test: :list
40
+ end
41
+
42
+ describe :read do
43
+ context 'when fetching a specific site', vcr: {cassette_name: 'site/read'} do
44
+ let(:response) { {"publicKey"=>"1cu4vdx7a6m9c1jc2425k1o9r8me2mrz", "privateKey"=>"1khj0vgpcqtjfwbbl5fr3wiykmr2ajro", "url"=>"http://url", "email"=>"an-email@gmail.com", "platformName"=>"Drupal", "platformVersion"=>"6.20", "expectedLanguages"=>nil} }
45
+
46
+ it 'should return the site' do
47
+ expect(MollomRestApi::V1::Site.read('1cu4vdx7a6m9c1jc2425k1o9r8me2mrz')).to eq(response)
48
+ end
49
+ end
50
+
51
+ include_examples "api error handling", class_under_test: MollomRestApi::V1::Site, method_under_test: :read, method_args: ['a_public_key']
52
+ end
53
+
54
+ describe :delete do
55
+ context 'when deleting a specific site', vcr: {cassette_name: 'site/delete'} do
56
+ it 'should delete a site' do
57
+ expect{MollomRestApi::V1::Site.delete('1cu4vdx7a6m9c1jc2425k1o9r8me2mrz')}.not_to raise_error
58
+ end
59
+ end
60
+
61
+ include_examples "api error handling", class_under_test: MollomRestApi::V1::Site, method_under_test: :delete, method_args: ['a_public_key']
62
+ end
63
+ end
@@ -0,0 +1,64 @@
1
+ require "spec_helper"
2
+
3
+ describe MollomRestApi::V1::Whitelist do
4
+ describe :create do
5
+ context "when providing a valid url and email", vcr: {cassette_name: "whitelist/create"} do
6
+ let(:request_parameters) { {note: 'patate'} }
7
+ let(:response) {{"context"=>"authorIp", "created"=>"1412787909465", "id"=>"76747", "lastMatch"=>"0", "matchCount"=>"0", "status"=>"1", "value"=>"123.123.80.22"}}
8
+
9
+ it "should return a json response containing the whitelist entry" do
10
+ expect(MollomRestApi::V1::Whitelist.create('1lgj17lzuezlu1bn9ry4k3qz4k8nr42n', '123.123.80.22', 'authorIp', request_parameters)).to eq(response)
11
+ end
12
+ end
13
+
14
+ include_examples "api error handling", class_under_test: MollomRestApi::V1::Whitelist, method_under_test: :create, method_args: %w(public_key value context)
15
+ end
16
+
17
+ describe :update do
18
+ context "when providing a public key and a value for an author name", vcr: {cassette_name: "whitelist/update"} do
19
+ let(:request_parameters) { {value: 'patate', context: 'authorName'} }
20
+ let(:response) {{"context"=>"authorName", "created"=>"1412787909465", "id"=>"76747", "lastMatch"=>"0", "matchCount"=>"0", "status"=>"0", "value"=>"patate"}}
21
+
22
+ it "should return a json response containing the updated whitelist entry" do
23
+ expect(MollomRestApi::V1::Whitelist.update('1lgj17lzuezlu1bn9ry4k3qz4k8nr42n', '76747', request_parameters)).to eq(response)
24
+ end
25
+ end
26
+
27
+ include_examples "api error handling", class_under_test: MollomRestApi::V1::Whitelist, method_under_test: :update, method_args: %w(public_key whitelist_entry_id)
28
+ end
29
+
30
+ describe :delete do
31
+ context "when providing a public key and whitelist entry id", vcr: {cassette_name: "whitelist/delete"} do
32
+ it "should return a json response containing the deleted whitelist entry" do
33
+ expect{MollomRestApi::V1::Whitelist.delete('1lgj17lzuezlu1bn9ry4k3qz4k8nr42n', '79552')}.not_to raise_error
34
+ end
35
+ end
36
+
37
+ include_examples "api error handling", class_under_test: MollomRestApi::V1::Whitelist, method_under_test: :delete, method_args: %w(public_key whitelist_entry_id)
38
+ end
39
+
40
+ describe :list do
41
+ context "when providing a public key", vcr: {cassette_name: "whitelist/list"} do
42
+ let(:request_parameters) { {count: 5} }
43
+ let(:response) {{"entry"=>{"context"=>"authorName", "created"=>"1412787909465", "id"=>"76747", "lastMatch"=>"0", "matchCount"=>"0", "status"=>"0", "value"=>"patate"}}}
44
+
45
+ it "should return a json response containing the list of whitelist entries" do
46
+ expect(MollomRestApi::V1::Whitelist.list('1lgj17lzuezlu1bn9ry4k3qz4k8nr42n', request_parameters)).to eq(response)
47
+ end
48
+ end
49
+
50
+ include_examples "api error handling", class_under_test: MollomRestApi::V1::Whitelist, method_under_test: :list, method_args: %w(a_public_key)
51
+ end
52
+
53
+ describe :read do
54
+ context "when providing a public key and whitelist entry id", vcr: {cassette_name: "whitelist/read"} do
55
+ let(:response) {{"context"=>"authorName", "created"=>"1413826721455", "id"=>"79557", "lastMatch"=>"0", "matchCount"=>"0", "status"=>"1", "value"=>"patate"}}
56
+
57
+ it "should return a json response containing the whitelist entry" do
58
+ expect(MollomRestApi::V1::Whitelist.read('1lgj17lzuezlu1bn9ry4k3qz4k8nr42n', '79557')).to eq(response)
59
+ end
60
+ end
61
+
62
+ include_examples "api error handling", class_under_test: MollomRestApi::V1::Whitelist, method_under_test: :read, method_args: %w(public_key whitelist_entry_id)
63
+ end
64
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe MollomRestApi::V1 do
4
+ include AlternateSyntaxMacro
5
+
6
+ describe 'when calling the alternate syntax' do
7
+ it_should_provide_an_alternate_syntax_to_api_calls
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ describe MollomRestApi do
4
+ specify { expect(MollomRestApi::VERSION).to_not be_nil }
5
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe MollomRestApi::VersionedApi do
4
+ describe 'when calling the alternate syntax' do
5
+ describe :create_ticket do
6
+ it "should call MollomRestApi::VTest01::Ticket.create" do
7
+ expect(MollomRestApi::VTest01::Ticket).to receive(:create).once
8
+ MollomRestApi::VTest01.create_ticket
9
+ end
10
+
11
+ it "should specify that it responds to that method" do
12
+ expect(MollomRestApi::VTest01).to respond_to(:create_ticket)
13
+ end
14
+ end
15
+
16
+ describe :delete_api do
17
+ it "should try to call MollomRestApi::VTest01::Api.delete" do
18
+ expect{MollomRestApi::VTest01.delete_api}.to raise_exception(NameError)
19
+ end
20
+
21
+ it "should not be able to respond to delete_api" do
22
+ expect(MollomRestApi::VTest01).not_to respond_to(:delete_api)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe MollomRestApi do
4
+ describe :attr_accessors do
5
+ %w(url public_key private_key oauth_options).each do |config|
6
+ describe config do
7
+ before(:each) { @old_config = MollomRestApi.send(config) }
8
+ after(:each) { MollomRestApi.send("#{config}=", @old_config) }
9
+
10
+ it "should have an attr_accessor for #{config}" do
11
+ MollomRestApi.send("#{config}=", 'test')
12
+ expect(MollomRestApi.send(config)).to eq('test')
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ describe :oauth_access_token do
19
+ let(:oauth_access_token) { MollomRestApi.oauth_access_token }
20
+
21
+ it "should return a oauth access token" do
22
+ expect(oauth_access_token).to be_a(OAuth::AccessToken)
23
+
24
+ expect(oauth_access_token.consumer).to be_a(OAuth::Consumer)
25
+ expect(oauth_access_token.consumer.key).to eq('my_public_key')
26
+ expect(oauth_access_token.consumer.secret).to eq('my_private_key')
27
+ expect(oauth_access_token.consumer.options).to include(proxy: 'http://proxy.com:8888')
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,15 @@
1
+ require 'mollom_rest_api'
2
+ require 'fixtures/v_test_01'
3
+ Dir["./spec/support/**/*.rb"].sort.each { |f| require f}
4
+
5
+ require 'coveralls'
6
+ Coveralls.wear!
7
+
8
+ RSpec.configure do |c|
9
+ c.before(:all) do
10
+ MollomRestApi.url = 'http://dev.mollom.com'
11
+ MollomRestApi.public_key = 'my_public_key'
12
+ MollomRestApi.private_key = 'my_private_key'
13
+ MollomRestApi.oauth_options = {proxy: 'http://proxy.com:8888'}
14
+ end
15
+ end
@@ -0,0 +1,38 @@
1
+ module AlternateSyntaxMacro
2
+ module ExampleGroupMethods
3
+ def it_should_provide_an_alternate_syntax_to_api_calls
4
+ available_apis = described_class.constants
5
+
6
+ available_apis.each do |api|
7
+ class_name = "#{described_class.name}::#{api}"
8
+ target_class = class_name.constantize
9
+ class_methods = receiver_public_methods_of(target_class)
10
+
11
+ class_methods.each do |action|
12
+ alternate_syntax = "#{action}_#{api.downcase}"
13
+
14
+ describe alternate_syntax do
15
+ it "should call #{class_name}.#{action}" do
16
+ expect(target_class).to receive(action).once
17
+ described_class.send(alternate_syntax)
18
+ end
19
+
20
+ it "should specify that it responds to that method" do
21
+ expect(described_class).to respond_to(alternate_syntax)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def receiver_public_methods_of(klass)
31
+ klass.public_methods(false) & klass.methods(false)
32
+ end
33
+ end
34
+
35
+ def self.included(receiver)
36
+ receiver.extend ExampleGroupMethods
37
+ end
38
+ end
@@ -0,0 +1,33 @@
1
+ shared_examples "api error handling" do |options = {}|
2
+ let(:class_under_test) { options.fetch(:class_under_test) }
3
+ let(:method_under_test) { options.fetch(:method_under_test) }
4
+
5
+ context "when there is a missing configuration" do
6
+ MollomRestApi::Interface::MANDATORY_CONFIGURATIONS.each do |config|
7
+ context "when there #{config} is missing" do
8
+ before(:each) do
9
+ @old_config = MollomRestApi.send(config)
10
+ MollomRestApi.send("#{config}=", nil)
11
+ end
12
+ after(:each) { MollomRestApi.send("#{config}=", @old_config) }
13
+
14
+ it "should raise an exception" do
15
+ expect{class_under_test.send(method_under_test, *options[:method_args])}.to raise_exception do |exception|
16
+ expect(exception).to be_a(MollomRestApi::MissingConfig)
17
+ expect(exception.message).to eq("Missing #{config}.")
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ context "when there is an error with the api response", vcr: {cassette_name: "invalid_request", match_requests_on: [:method, :base_uri]} do
25
+ it "should raise an exception" do
26
+ expect{class_under_test.send(method_under_test, *options[:method_args])}.to raise_exception do |exception|
27
+ expect(exception).to be_a(MollomRestApi::ApiException)
28
+ expect(exception.message).to eq("{\"code\":\"401\",\"message\":\"Error.\"}")
29
+ expect(exception.error_code).to eq(401)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,37 @@
1
+ require 'webmock/rspec'
2
+ require 'vcr'
3
+
4
+ module VCRHelper
5
+ PARAMS_TO_STRIP = %w(oauth_consumer_key oauth_nonce oauth_signature)
6
+
7
+ def strip_authorization(headers)
8
+ headers['Authorization'] = headers['Authorization'].collect do |header|
9
+ header.gsub(%r((#{params_to_strip})="(.*)")) { "#{$1}=\"\"" }
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def params_to_strip
16
+ PARAMS_TO_STRIP.join('|')
17
+ end
18
+ end
19
+
20
+ VCR.configure do |c|
21
+ include VCRHelper
22
+
23
+ c.cassette_library_dir = 'spec/fixtures/cassette_library'
24
+ c.debug_logger = File.open('log/test.log', 'w')
25
+ c.hook_into :webmock
26
+ c.ignore_localhost = true
27
+ c.default_cassette_options = {record: :none, match_requests_on: [:method, :uri, :body, :headers_with_striped_auth]}
28
+ c.configure_rspec_metadata!
29
+
30
+ c.register_request_matcher :headers_with_striped_auth do |r1, r2|
31
+ strip_authorization(r1.headers.to_hash) == strip_authorization(r2.headers.to_hash)
32
+ end
33
+
34
+ c.register_request_matcher :base_uri do |r1, r2|
35
+ r1.uri.start_with?(r2.uri)
36
+ end
37
+ end