discourse_api 2.0.0 → 2.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 (34) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +6 -6
  3. data/.rubocop.yml +3 -0
  4. data/.streerc +1 -1
  5. data/CHANGELOG.md +10 -0
  6. data/config_example.yml +1 -1
  7. data/discourse_api.gemspec +36 -31
  8. data/lib/discourse_api/api/categories.rb +45 -54
  9. data/lib/discourse_api/api/topics.rb +13 -1
  10. data/lib/discourse_api/api/users.rb +0 -4
  11. data/lib/discourse_api/single_sign_on.rb +3 -3
  12. data/lib/discourse_api/version.rb +1 -1
  13. data/spec/discourse_api/api/api_key_spec.rb +11 -11
  14. data/spec/discourse_api/api/backups_spec.rb +3 -3
  15. data/spec/discourse_api/api/badges_spec.rb +5 -5
  16. data/spec/discourse_api/api/categories_spec.rb +37 -15
  17. data/spec/discourse_api/api/email_spec.rb +5 -5
  18. data/spec/discourse_api/api/groups_spec.rb +17 -17
  19. data/spec/discourse_api/api/invite_spec.rb +11 -11
  20. data/spec/discourse_api/api/notifications_spec.rb +3 -3
  21. data/spec/discourse_api/api/polls_spec.rb +7 -7
  22. data/spec/discourse_api/api/posts_spec.rb +1 -3
  23. data/spec/discourse_api/api/private_messages_spec.rb +6 -6
  24. data/spec/discourse_api/api/search_spec.rb +5 -5
  25. data/spec/discourse_api/api/site_settings_spec.rb +2 -2
  26. data/spec/discourse_api/api/sso_spec.rb +3 -3
  27. data/spec/discourse_api/api/topics_spec.rb +32 -32
  28. data/spec/discourse_api/api/uploads_spec.rb +3 -3
  29. data/spec/discourse_api/api/user_actions_spec.rb +5 -5
  30. data/spec/discourse_api/api/users_spec.rb +51 -51
  31. data/spec/discourse_api/client_spec.rb +33 -33
  32. data/spec/discourse_api/single_sign_on_spec.rb +23 -6
  33. data/spec/spec_helper.rb +1 -0
  34. metadata +9 -26
@@ -2,7 +2,7 @@
2
2
  require "spec_helper"
3
3
 
4
4
  describe DiscourseApi::Client do
5
- subject { DiscourseApi::Client.new(host) }
5
+ subject(:client) { DiscourseApi::Client.new(host) }
6
6
 
7
7
  describe ".new" do
8
8
  it "requires a host argument" do
@@ -10,11 +10,11 @@ describe DiscourseApi::Client do
10
10
  end
11
11
 
12
12
  it "defaults api key to nil" do
13
- expect(subject.api_key).to be_nil
13
+ expect(client.api_key).to be_nil
14
14
  end
15
15
 
16
16
  it "defaults api username to nil" do
17
- expect(subject.api_username).to be_nil
17
+ expect(client.api_username).to be_nil
18
18
  end
19
19
 
20
20
  it "accepts an api key argument" do
@@ -31,55 +31,55 @@ describe DiscourseApi::Client do
31
31
  describe "#timeout" do
32
32
  context "with a custom timeout" do
33
33
  it "is set to Faraday connection" do
34
- expect(subject.send(:connection).options.timeout).to eq(30)
34
+ expect(client.send(:connection).options.timeout).to eq(30)
35
35
  end
36
36
  end
37
37
 
38
38
  context "with the default timeout" do
39
39
  it "is set to Faraday connection" do
40
- subject.timeout = 25
41
- expect(subject.send(:connection).options.timeout).to eq(25)
40
+ client.timeout = 25
41
+ expect(client.send(:connection).options.timeout).to eq(25)
42
42
  end
43
43
  end
44
44
 
45
45
  it "raises DiscourseApi::Timeout" do
46
46
  stub_get("#{host}/t/1.json").to_timeout
47
47
 
48
- expect { subject.topic(1) }.to raise_error(DiscourseApi::Timeout)
48
+ expect { client.topic(1) }.to raise_error(DiscourseApi::Timeout)
49
49
  end
50
50
  end
51
51
 
52
52
  describe "#api_key" do
53
53
  it "is publicly accessible" do
54
- subject.api_key = "test_d7fd0429940"
55
- expect(subject.api_key).to eq("test_d7fd0429940")
54
+ client.api_key = "test_d7fd0429940"
55
+ expect(client.api_key).to eq("test_d7fd0429940")
56
56
  end
57
57
  end
58
58
 
59
59
  describe "#api_username" do
60
60
  it "is publicly accessible" do
61
- subject.api_username = "test_user"
62
- expect(subject.api_username).to eq("test_user")
61
+ client.api_username = "test_user"
62
+ expect(client.api_username).to eq("test_user")
63
63
  end
64
64
  end
65
65
 
66
66
  describe "#host" do
67
67
  it "is publicly readable" do
68
- expect(subject.host).to eq("#{host}")
68
+ expect(client.host).to eq("#{host}")
69
69
  end
70
70
 
71
71
  it "is not publicly writeable" do
72
- expect(subject).not_to respond_to(:host=)
72
+ expect(client).not_to respond_to(:host=)
73
73
  end
74
74
  end
75
75
 
76
76
  describe "#connection" do
77
77
  it "looks like a Faraday connection" do
78
- expect(subject.send(:connection)).to respond_to :run_request
78
+ expect(client.send(:connection)).to respond_to :run_request
79
79
  end
80
80
 
81
81
  it "memorizes the connection" do
82
- c1, c2 = subject.send(:connection), subject.send(:connection)
82
+ c1, c2 = client.send(:connection), client.send(:connection)
83
83
  expect(c1.object_id).to eq(c2.object_id)
84
84
  end
85
85
  end
@@ -87,22 +87,22 @@ describe DiscourseApi::Client do
87
87
  describe "#delete" do
88
88
  before do
89
89
  stub_delete("#{host}/test/delete").with(query: { deleted: "object" })
90
- subject.api_key = "test_d7fd0429940"
91
- subject.api_username = "test_user"
90
+ client.api_key = "test_d7fd0429940"
91
+ client.api_username = "test_user"
92
92
  end
93
93
 
94
94
  it "allows custom delete requests" do
95
- subject.delete("/test/delete", { deleted: "object" })
95
+ client.delete("/test/delete", { deleted: "object" })
96
96
  expect(a_delete("#{host}/test/delete").with(query: { deleted: "object" })).to have_been_made
97
97
  end
98
98
 
99
99
  context "when using a host with a subdirectory" do
100
- subject { DiscourseApi::Client.new("#{host}/forum") }
100
+ subject(:client) { DiscourseApi::Client.new("#{host}/forum") }
101
101
 
102
102
  before { stub_delete("#{host}/forum/test/delete").with(query: { deleted: "object" }) }
103
103
 
104
104
  it "allows custom delete requests" do
105
- subject.delete("/test/delete", { deleted: "object" })
105
+ client.delete("/test/delete", { deleted: "object" })
106
106
  expect(
107
107
  a_delete("#{host}/forum/test/delete").with(query: { deleted: "object" }),
108
108
  ).to have_been_made
@@ -113,22 +113,22 @@ describe DiscourseApi::Client do
113
113
  describe "#post" do
114
114
  before do
115
115
  stub_post("#{host}/test/post").with(body: { created: "object" })
116
- subject.api_key = "test_d7fd0429940"
117
- subject.api_username = "test_user"
116
+ client.api_key = "test_d7fd0429940"
117
+ client.api_username = "test_user"
118
118
  end
119
119
 
120
120
  it "allows custom post requests" do
121
- subject.post("/test/post", { created: "object" })
121
+ client.post("/test/post", { created: "object" })
122
122
  expect(a_post("#{host}/test/post").with(body: { created: "object" })).to have_been_made
123
123
  end
124
124
 
125
125
  context "when using a host with a subdirectory" do
126
- subject { DiscourseApi::Client.new("#{host}/forum") }
126
+ subject(:client) { DiscourseApi::Client.new("#{host}/forum") }
127
127
 
128
128
  before { stub_post("#{host}/forum/test/post").with(body: { created: "object" }) }
129
129
 
130
130
  it "allows custom post requests" do
131
- subject.post("/test/post", { created: "object" })
131
+ client.post("/test/post", { created: "object" })
132
132
  expect(
133
133
  a_post("#{host}/forum/test/post").with(body: { created: "object" }),
134
134
  ).to have_been_made
@@ -139,22 +139,22 @@ describe DiscourseApi::Client do
139
139
  describe "#put" do
140
140
  before do
141
141
  stub_put("#{host}/test/put").with(body: { updated: "object" })
142
- subject.api_key = "test_d7fd0429940"
143
- subject.api_username = "test_user"
142
+ client.api_key = "test_d7fd0429940"
143
+ client.api_username = "test_user"
144
144
  end
145
145
 
146
146
  it "allows custom put requests" do
147
- subject.put("/test/put", { updated: "object" })
147
+ client.put("/test/put", { updated: "object" })
148
148
  expect(a_put("#{host}/test/put").with(body: { updated: "object" })).to have_been_made
149
149
  end
150
150
 
151
151
  context "when using a host with a subdirectory" do
152
- subject { DiscourseApi::Client.new("#{host}/forum") }
152
+ subject(:client) { DiscourseApi::Client.new("#{host}/forum") }
153
153
 
154
154
  before { stub_put("#{host}/forum/test/put").with(body: { updated: "object" }) }
155
155
 
156
156
  it "allows custom post requests" do
157
- subject.put("/test/put", { updated: "object" })
157
+ client.put("/test/put", { updated: "object" })
158
158
  expect(a_put("#{host}/forum/test/put").with(body: { updated: "object" })).to have_been_made
159
159
  end
160
160
  end
@@ -167,17 +167,17 @@ describe DiscourseApi::Client do
167
167
  OpenStruct.new(env: { body: "error page html" }, status: 500),
168
168
  )
169
169
  allow(Faraday).to receive(:new).and_return(connection)
170
- expect { subject.send(:request, :get, "/test") }.to raise_error DiscourseApi::Error
170
+ expect { client.send(:request, :get, "/test") }.to raise_error DiscourseApi::Error
171
171
  end
172
172
 
173
173
  it "catches Faraday errors" do
174
174
  allow(Faraday).to receive(:new).and_raise(Faraday::ClientError.new("BOOM!"))
175
- expect { subject.send(:request, :get, "/test") }.to raise_error DiscourseApi::Error
175
+ expect { client.send(:request, :get, "/test") }.to raise_error DiscourseApi::Error
176
176
  end
177
177
 
178
178
  it "catches JSON::ParserError errors" do
179
179
  allow(Faraday).to receive(:new).and_raise(JSON::ParserError.new("unexpected token"))
180
- expect { subject.send(:request, :get, "/test") }.to raise_error DiscourseApi::Error
180
+ expect { client.send(:request, :get, "/test") }.to raise_error DiscourseApi::Error
181
181
  end
182
182
  end
183
183
  end
@@ -32,12 +32,29 @@ describe DiscourseApi::SingleSignOn do
32
32
  end
33
33
 
34
34
  describe ".parse" do
35
- it "raises ParseError when there's a signature mismatch" do
36
- sso = described_class.new
37
- sso.sso_secret = "abcd"
38
- expect { described_class.parse(sso.payload, "dcba") }.to raise_error(
39
- DiscourseApi::SingleSignOn::ParseError,
40
- )
35
+ context "when sso is present" do
36
+ it "raises ParseError when there's a signature mismatch" do
37
+ sso = described_class.new
38
+ sso.sso_secret = "abcd"
39
+ expect { described_class.parse(sso.payload, "dcba") }.to raise_error(
40
+ DiscourseApi::SingleSignOn::ParseError,
41
+ )
42
+ end
43
+ end
44
+
45
+ context "when sso is missing" do
46
+ it "raises ParseError when there's a signature mismatch" do
47
+ sso = described_class.new
48
+ sso.sso_secret = "abcd"
49
+ missing_sso = Rack::Utils.parse_query(sso.payload)
50
+ missing_sso.delete("sso")
51
+ malformed_query = Rack::Utils.build_query(missing_sso)
52
+
53
+ expect { described_class.parse(malformed_query, "dcba") }.to raise_error(
54
+ DiscourseApi::SingleSignOn::ParseError,
55
+ /The SSO field should/i,
56
+ )
57
+ end
41
58
  end
42
59
  end
43
60
  end
data/spec/spec_helper.rb CHANGED
@@ -9,6 +9,7 @@ SimpleCov.start { add_filter "/spec/" }
9
9
  require "discourse_api"
10
10
  require "rspec"
11
11
  require "webmock/rspec"
12
+ require "ostruct"
12
13
 
13
14
  RSpec.configure do |config|
14
15
  config.expect_with :rspec do |c|
metadata CHANGED
@@ -1,17 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: discourse_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Saffron
8
8
  - John Paul Ashenfelter
9
9
  - Michael Herold
10
10
  - Blake Erickson
11
- autorequire:
12
11
  bindir: bin
13
12
  cert_chain: []
14
- date: 2023-05-26 00:00:00.000000000 Z
13
+ date: 1980-01-02 00:00:00.000000000 Z
15
14
  dependencies:
16
15
  - !ruby/object:Gem::Dependency
17
16
  name: faraday
@@ -185,44 +184,30 @@ dependencies:
185
184
  name: rubocop-discourse
186
185
  requirement: !ruby/object:Gem::Requirement
187
186
  requirements:
188
- - - "~>"
187
+ - - '='
189
188
  - !ruby/object:Gem::Version
190
- version: 3.2.0
189
+ version: 3.8.1
191
190
  type: :development
192
191
  prerelease: false
193
192
  version_requirements: !ruby/object:Gem::Requirement
194
193
  requirements:
195
- - - "~>"
194
+ - - '='
196
195
  - !ruby/object:Gem::Version
197
- version: 3.2.0
196
+ version: 3.8.1
198
197
  - !ruby/object:Gem::Dependency
199
198
  name: syntax_tree
200
199
  requirement: !ruby/object:Gem::Requirement
201
200
  requirements:
202
201
  - - "~>"
203
202
  - !ruby/object:Gem::Version
204
- version: 6.1.1
205
- type: :development
206
- prerelease: false
207
- version_requirements: !ruby/object:Gem::Requirement
208
- requirements:
209
- - - "~>"
210
- - !ruby/object:Gem::Version
211
- version: 6.1.1
212
- - !ruby/object:Gem::Dependency
213
- name: syntax_tree-disable_ternary
214
- requirement: !ruby/object:Gem::Requirement
215
- requirements:
216
- - - "~>"
217
- - !ruby/object:Gem::Version
218
- version: 1.0.0
203
+ version: 6.2.0
219
204
  type: :development
220
205
  prerelease: false
221
206
  version_requirements: !ruby/object:Gem::Requirement
222
207
  requirements:
223
208
  - - "~>"
224
209
  - !ruby/object:Gem::Version
225
- version: 1.0.0
210
+ version: 6.2.0
226
211
  description: Discourse API
227
212
  email:
228
213
  - sam.saffron@gmail.com
@@ -376,7 +361,6 @@ homepage: http://github.com/discourse/discourse_api
376
361
  licenses:
377
362
  - MIT
378
363
  metadata: {}
379
- post_install_message:
380
364
  rdoc_options: []
381
365
  require_paths:
382
366
  - lib
@@ -391,8 +375,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
391
375
  - !ruby/object:Gem::Version
392
376
  version: '0'
393
377
  requirements: []
394
- rubygems_version: 3.1.6
395
- signing_key:
378
+ rubygems_version: 3.6.9
396
379
  specification_version: 4
397
380
  summary: Allows access to the Discourse API
398
381
  test_files: