discourse_api 0.4.0 → 0.4.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ef1ea595c7e724545877e4a688f7f192a1f63573
4
- data.tar.gz: f97ed183f55cae5b6cb01616264cabf5249bdbdb
3
+ metadata.gz: 8330a94a826de63e8b485e79b0ee6e43dfc0cbe2
4
+ data.tar.gz: d8145df00cfb45f265fe8fc2e3af9e1d0e35e230
5
5
  SHA512:
6
- metadata.gz: 31b68b7b2101e74ca1dcb84c8528c129b630cdd1ff74a1c2eeadc42529562fc8dcfb5f13ed899dc94fe8513f1e4c0c314d6609fe7a1f0c3eef5436cb344b9751
7
- data.tar.gz: eac6dbdd5cdecae47f0b2b4fac2a819b324437831b8aabb984db2f0b9f86efe0d4872b6a1970f67d2472665ce893ad13a09b238b60cff6c3726a7f94e31094f2
6
+ metadata.gz: f9ea65210238643e2233e73f28ea1945a877472c4581794c93cf208a99232b135669fa476a400f69ac43c39b6fdf47a0dd33189c67009da65a8137b23bfbdca6
7
+ data.tar.gz: f0753dc07dc1c480e2ee6e78a8565b96cf5605ea40bee94a5c3eed2022fb10b6c848cf9ff58f1eafb306e2a319ca0b181f9b3517f3e0a5424285de90985432bf
data/examples/groups.rb CHANGED
@@ -7,8 +7,11 @@ client = DiscourseApi::Client.new
7
7
  response = client.create_group(name: "engineering_team")
8
8
  group_id = response["basic_group"]["id"]
9
9
 
10
- client.group_add(group_id, "sam")
11
- client.group_add(group_id, "jeff")
12
- client.group_add(group_id, "neil")
10
+ client.group_add(group_id, username: "sam")
11
+ client.group_add(group_id, username: "jeff")
12
+ client.group_add(group_id, usernames: ["neil", "dan"])
13
+ client.group_add(group_id, user_id: 123)
14
+ client.group_add(group_id, user_ids: [123, 456])
13
15
 
14
- client.group_remove(group_id, "neil")
16
+ client.group_remove(group_id, username: "neil")
17
+ client.group_remove(group_id, user_id: 123)
@@ -0,0 +1,31 @@
1
+ module DiscourseApi
2
+ module API
3
+ module ApiKey
4
+ def api
5
+ response = get("/admin/api.json")
6
+ response.body
7
+ end
8
+
9
+ def generate_user_api_key(user_id)
10
+ response = post("/admin/users/#{user_id}/generate_api_key.json")
11
+ end
12
+
13
+ def revoke_user_api_key(user_id)
14
+ response = delete("/admin/users/#{user_id}/revoke_api_key.json")
15
+ end
16
+
17
+ def generate_master_key
18
+ response = post("/admin/api/key")
19
+ end
20
+
21
+ def revoke_api_key(id)
22
+ response = delete("/admin/api/key", {id: id})
23
+ end
24
+
25
+ def regenerate_api_key(id)
26
+ response = put("/admin/api/key", {id: id})
27
+ response.body
28
+ end
29
+ end
30
+ end
31
+ end
@@ -21,6 +21,16 @@ module DiscourseApi
21
21
  response[:body]['topic_list']['topics']
22
22
  end
23
23
 
24
+ def category_top_topics(category_slug)
25
+ response = get("/category/#{category_slug}/l/top.json")
26
+ response[:body]['topic_list']['topics']
27
+ end
28
+
29
+ def category_new_topics(category_slug)
30
+ response = get("/category/#{category_slug}/l/new.json")
31
+ response[:body]['topic_list']['topics']
32
+ end
33
+
24
34
  def category(id)
25
35
  response = get("/c/#{id}/show")
26
36
  response.body['category']
@@ -6,7 +6,7 @@ module DiscourseApi
6
6
  .required(:name)
7
7
  .default(visible: true)
8
8
  .to_h
9
- post("/admin/groups", group: args)
9
+ post("/admin/groups", args)
10
10
  end
11
11
 
12
12
  def groups
@@ -14,12 +14,25 @@ module DiscourseApi
14
14
  response.body
15
15
  end
16
16
 
17
- def group_add(group_id, *usernames)
18
- patch("/admin/groups/#{group_id}", changes: {add: usernames})
17
+ def group_add(group_id, users)
18
+ users.keys.each do |key|
19
+ # Accept arrays and convert to comma-delimited string.
20
+ if users[key].respond_to? :join
21
+ users[key] = users[key].join(",")
22
+ end
23
+
24
+ # Accept non-plural user_id or username, but send pluralized version in the request.
25
+ if key.to_s[-1] != 's'
26
+ users["#{key}s"] = users[key]
27
+ users.delete(key)
28
+ end
29
+ end
30
+
31
+ put("/admin/groups/#{group_id}/members.json", users)
19
32
  end
20
33
 
21
- def group_remove(group_id, *usernames)
22
- patch("/admin/groups/#{group_id}", changes: {delete: usernames})
34
+ def group_remove(group_id, user)
35
+ delete("/admin/groups/#{group_id}/members.json", user)
23
36
  end
24
37
  end
25
38
  end
@@ -59,6 +59,16 @@ module DiscourseApi
59
59
  response = get("admin/users/list/#{type}.json")
60
60
  response[:body]
61
61
  end
62
+
63
+ def grant_admin(user_id)
64
+ response = put("admin/users/#{user_id}/grant_admin")
65
+ response[:body]
66
+ end
67
+
68
+ def revoke_admin(user_id)
69
+ response = put("admin/users/#{user_id}/revoke_admin")
70
+ response[:body]
71
+ end
62
72
  end
63
73
  end
64
74
  end
@@ -15,7 +15,7 @@ require 'discourse_api/api/private_messages'
15
15
  require 'discourse_api/api/notifications'
16
16
  require 'discourse_api/api/badges'
17
17
  require 'discourse_api/api/email'
18
- require 'discourse_api/api/api_admin'
18
+ require 'discourse_api/api/api_key'
19
19
  require 'discourse_api/api/backups'
20
20
 
21
21
  module DiscourseApi
@@ -35,7 +35,7 @@ module DiscourseApi
35
35
  include DiscourseApi::API::Notifications
36
36
  include DiscourseApi::API::Badges
37
37
  include DiscourseApi::API::Email
38
- include DiscourseApi::API::ApiAdmin
38
+ include DiscourseApi::API::ApiKey
39
39
  include DiscourseApi::API::Backups
40
40
 
41
41
  def initialize(host = ENV["DISCOURSE_URL"],
@@ -114,9 +114,17 @@ module DiscourseApi
114
114
  params = params.to_h if params.respond_to? :to_h
115
115
  end
116
116
  response = connection.send(method.to_sym, path, params)
117
+ handle_error(response)
117
118
  response.env
118
119
  rescue Faraday::Error::ClientError, JSON::ParserError
119
120
  raise DiscourseApi::Error
120
121
  end
122
+
123
+ def handle_error(response)
124
+ case response.status
125
+ when 403
126
+ raise DiscourseApi::UnauthenticatedError.new(response.env[:body])
127
+ end
128
+ end
121
129
  end
122
130
  end
@@ -11,4 +11,7 @@ module DiscourseApi
11
11
  exception.respond_to?(:message) ? super(exception.message) : super(exception.to_s)
12
12
  end
13
13
  end
14
+
15
+ class UnauthenticatedError < StandardError
16
+ end
14
17
  end
@@ -1,3 +1,3 @@
1
1
  module DiscourseApi
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.1"
3
3
  end
@@ -0,0 +1,121 @@
1
+ require 'spec_helper'
2
+
3
+ describe DiscourseApi::API::ApiKey do
4
+ subject {
5
+ DiscourseApi::Client.new(
6
+ "http://localhost:3000",
7
+ "test_d7fd0429940",
8
+ "test_user"
9
+ )
10
+ }
11
+
12
+ describe "#api" do
13
+ before do
14
+ url = "http://localhost:3000/admin/api.json" +
15
+ "?api_key=test_d7fd0429940&api_username=test_user"
16
+ stub_get(url).to_return(body: fixture("api.json"),
17
+ headers: { content_type: "application/json" })
18
+ end
19
+
20
+ it "requests the correct resource" do
21
+ subject.api
22
+ url = "http://localhost:3000/admin/api.json" +
23
+ "?api_key=test_d7fd0429940&api_username=test_user"
24
+ expect(a_get(url)).to have_been_made
25
+ end
26
+
27
+ it "returns the requested api keys" do
28
+ api = subject.api
29
+ expect(api).to be_an Array
30
+ expect(api.first).to be_a Hash
31
+ expect(api.first).to have_key('key')
32
+ end
33
+ end
34
+
35
+ describe "#generate_user_api_key" do
36
+ before do
37
+ url = "http://localhost:3000/admin/users/2/generate_api_key.json" +
38
+ "?api_key=test_d7fd0429940&api_username=test_user"
39
+ stub_post(url).to_return(body: fixture("generate_api_key.json"),
40
+ headers: { content_type: "application/json" })
41
+ end
42
+
43
+ it "returns the generated api key" do
44
+ api_key = subject.generate_user_api_key(2)
45
+ expect(api_key).to be_a Hash
46
+ expect(api_key['api_key']).to have_key('key')
47
+ end
48
+ end
49
+
50
+ describe "#revoke_user_api_key" do
51
+ before do
52
+ url = "http://localhost:3000/admin/users/2/revoke_api_key.json" +
53
+ "?api_key=test_d7fd0429940&api_username=test_user"
54
+ stub_delete(url).to_return(body: "",
55
+ headers: { content_type: "application/json" })
56
+ end
57
+
58
+ it "returns 200" do
59
+ response = subject.revoke_user_api_key(2)
60
+ expect(response['status']).to eq(200)
61
+ end
62
+ end
63
+
64
+ describe "#generate_master_key" do
65
+ before do
66
+ url = "http://localhost:3000/admin/api/key" +
67
+ "?api_key=test_d7fd0429940&api_username=test_user"
68
+ stub_post(url).to_return(body: fixture("generate_master_key.json"),
69
+ headers: { content_type: "application/json" })
70
+ end
71
+
72
+ it "returns the generated master key" do
73
+ master_key = subject.generate_master_key
74
+ expect(master_key).to be_a Hash
75
+ expect(master_key['api_key']).to have_key('key')
76
+ expect(master_key['api_key']['user']).to eq(nil)
77
+ end
78
+ end
79
+
80
+ describe "#revoke_api_key" do
81
+ before do
82
+ url = "http://localhost:3000/admin/api/key" +
83
+ "?api_key=test_d7fd0429940&api_username=test_user&id=10"
84
+ stub_delete(url).to_return(body: "",
85
+ headers: { content_type: "application/json" })
86
+ end
87
+
88
+ it "requests the correct resource" do
89
+ subject.revoke_api_key(10)
90
+ url = "http://localhost:3000/admin/api/key" +
91
+ "?api_key=test_d7fd0429940&api_username=test_user&id=10"
92
+ expect(a_delete(url)).to have_been_made
93
+ end
94
+
95
+ it "returns 200" do
96
+ response = subject.revoke_api_key(10)
97
+ expect(response['status']).to eq(200)
98
+ end
99
+ end
100
+
101
+ describe "#regenerate_api_key" do
102
+ before do
103
+ url = "http://localhost:3000/admin/api/key" +
104
+ "?api_key=test_d7fd0429940&api_username=test_user"
105
+ stub_put(url).to_return(body: fixture("regenerate_api_key.json"),
106
+ headers: { content_type: "application/json" })
107
+ end
108
+
109
+ it "requests the correct resource" do
110
+ subject.regenerate_api_key(10)
111
+ url = "http://localhost:3000/admin/api/key" +
112
+ "?api_key=test_d7fd0429940&api_username=test_user"
113
+ expect(a_put(url)).to have_been_made
114
+ end
115
+
116
+ it "returns the regenerated api key" do
117
+ key = subject.regenerate_api_key(10)
118
+ expect(key['api_key']).to have_key('key')
119
+ end
120
+ end
121
+ end
@@ -36,4 +36,34 @@ describe DiscourseApi::API::Categories do
36
36
  expect(latest_topics).to be_an Array
37
37
  end
38
38
  end
39
+
40
+ describe '#category_top_topics' do
41
+ before do
42
+ stub_get("http://localhost:3000/category/category-slug/l/top.json?api_key=test_d7fd0429940&api_username=test_user")
43
+ .to_return(
44
+ body: fixture("category_topics.json"),
45
+ headers: { content_type: "application/json" }
46
+ )
47
+ end
48
+
49
+ it "returns the top topics in a category" do
50
+ topics = subject.category_top_topics('category-slug')
51
+ expect(topics).to be_an Array
52
+ end
53
+ end
54
+
55
+ describe '#category_new_topics' do
56
+ before do
57
+ stub_get("http://localhost:3000/category/category-slug/l/new.json?api_key=test_d7fd0429940&api_username=test_user")
58
+ .to_return(
59
+ body: fixture("category_topics.json"),
60
+ headers: { content_type: "application/json" }
61
+ )
62
+ end
63
+
64
+ it "returns the new topics in a category" do
65
+ topics = subject.category_new_topics('category-slug')
66
+ expect(topics).to be_an Array
67
+ end
68
+ end
39
69
  end
@@ -23,24 +23,56 @@ describe DiscourseApi::API::Groups do
23
23
  stub_post("http://localhost:3000/admin/groups?api_key=test_d7fd0429940&api_username=test_user")
24
24
  subject.create_group(name: "test_group")
25
25
  expect(a_post("http://localhost:3000/admin/groups?api_key=test_d7fd0429940&api_username=test_user").
26
- with(body: {group: {name: "test_group", visible: "true"}})
26
+ with(body: {name: "test_group", visible: "true"})
27
27
  ).to have_been_made
28
28
  end
29
29
 
30
- it "adds members" do
31
- stub_request(:patch, "http://localhost:3000/admin/groups/123?api_key=test_d7fd0429940&api_username=test_user")
32
- subject.group_add(123, "sam")
33
- expect(a_request(:patch, "http://localhost:3000/admin/groups/123?api_key=test_d7fd0429940&api_username=test_user").
34
- with(body: {changes: {add: [ "sam" ]}})
35
- ).to have_been_made
30
+ describe "add members" do
31
+ before do
32
+ stub_request(:put, "http://localhost:3000/admin/groups/123/members.json?api_key=test_d7fd0429940&api_username=test_user")
33
+ end
34
+
35
+ it "adds a single member by username" do
36
+ subject.group_add(123, username: "sam")
37
+ expect(a_request(:put, "http://localhost:3000/admin/groups/123/members.json?api_key=test_d7fd0429940&api_username=test_user").
38
+ with(body: {usernames: "sam"})
39
+ ).to have_been_made
40
+ end
41
+
42
+ it "adds an array of members by username" do
43
+ subject.group_add(123, usernames: ["sam", "jeff"])
44
+ expect(a_request(:put, "http://localhost:3000/admin/groups/123/members.json?api_key=test_d7fd0429940&api_username=test_user").
45
+ with(body: {usernames: "sam,jeff"})
46
+ ).to have_been_made
47
+ end
48
+
49
+ it "adds a single member by user_id" do
50
+ subject.group_add(123, user_id: 456)
51
+ expect(a_request(:put, "http://localhost:3000/admin/groups/123/members.json?api_key=test_d7fd0429940&api_username=test_user").
52
+ with(body: {user_ids: "456"})
53
+ ).to have_been_made
54
+ end
55
+
56
+ it "adds an array of members by user_id" do
57
+ subject.group_add(123, user_id: [123, 456])
58
+ expect(a_request(:put, "http://localhost:3000/admin/groups/123/members.json?api_key=test_d7fd0429940&api_username=test_user").
59
+ with(body: {user_ids: "123,456"})
60
+ ).to have_been_made
61
+ end
36
62
  end
37
63
 
38
- it "removes members" do
39
- stub_request(:patch, "http://localhost:3000/admin/groups/123?api_key=test_d7fd0429940&api_username=test_user")
40
- subject.group_remove(123, "sam")
41
- expect(a_request(:patch, "http://localhost:3000/admin/groups/123?api_key=test_d7fd0429940&api_username=test_user").
42
- with(body: {changes: {delete: [ "sam" ]}})
43
- ).to have_been_made
64
+ describe "remove members" do
65
+ before do
66
+ url = "http://localhost:3000/admin/groups/123/members.json?api_key=test_d7fd0429940&api_username=test_user&username=sam"
67
+ stub_delete(url)
68
+ end
69
+
70
+ it "removes member" do
71
+ subject.group_remove(123, username: "sam")
72
+ expect(a_delete("http://localhost:3000/admin/groups/123/members.json?api_key=test_d7fd0429940&api_username=test_user&username=sam")).to have_been_made
73
+ end
44
74
  end
75
+
76
+
45
77
  end
46
78
  end
@@ -170,7 +170,7 @@ describe DiscourseApi::API::Users do
170
170
  describe "#update_trust_level" do
171
171
  before do
172
172
  url = "http://localhost:3000/admin/users/2/trust_level?api_key=test_d7fd0429940&api_username=test_user"
173
- stub_get(url).to_return(body: fixture("update_trust_level.json"),
173
+ stub_put(url).to_return(body: fixture("update_trust_level.json"),
174
174
  headers: { content_type: "application/json" })
175
175
  end
176
176
 
@@ -188,4 +188,24 @@ describe DiscourseApi::API::Users do
188
188
  expect(admin_user['admin_user']).to have_key('trust_level')
189
189
  end
190
190
  end
191
+
192
+ describe "#grant admin" do
193
+ before do
194
+ url = "http://localhost:3000/admin/users/11/grant_admin?api_key=test_d7fd0429940&api_username=test_user"
195
+ stub_put(url).to_return(body: fixture("user_grant_admin.json"),
196
+ headers: { content_type: "application/json" })
197
+ end
198
+
199
+ it "makes the correct put request" do
200
+ result = subject.grant_admin(11)
201
+ url = "http://localhost:3000/admin/users/11/grant_admin?api_key=test_d7fd0429940&api_username=test_user"
202
+ expect(a_put(url)).to have_been_made
203
+ end
204
+
205
+ it "makes the user an admin" do
206
+ result = subject.grant_admin(11)
207
+ expect(result).to be_a Hash
208
+ expect(result['admin_user']['admin']).to eq(true)
209
+ end
210
+ end
191
211
  end
@@ -0,0 +1,92 @@
1
+ {
2
+ "users": [
3
+ {
4
+ "id": 3,
5
+ "username": "fuzzy",
6
+ "avatar_template": "//www.gravatar.com/avatar/c0c59575e86a794d733db4ee29b2baf4.png?s={size}&r=pg&d=identicon"
7
+ }
8
+ ],
9
+ "topic_list": {
10
+ "can_create_topic": true,
11
+ "draft": null,
12
+ "draft_key": "new_topic",
13
+ "draft_sequence": 2,
14
+ "topics": [
15
+ {
16
+ "id": 5,
17
+ "title": "About the category",
18
+ "fancy_title": "About the category",
19
+ "slug": "about-the-category",
20
+ "posts_count": 2,
21
+ "reply_count": 0,
22
+ "highest_post_number": 2,
23
+ "image_url": null,
24
+ "created_at": "2014-04-16T14:10:50.950-04:00",
25
+ "last_posted_at": "2014-04-16T14:31:10.406-04:00",
26
+ "bumped": true,
27
+ "bumped_at": "2014-04-17T13:35:01.102-04:00",
28
+ "unseen": false,
29
+ "last_read_post_number": 2,
30
+ "unread": 0,
31
+ "new_posts": 0,
32
+ "pinned": true,
33
+ "unpinned": null,
34
+ "excerpt": null,
35
+ "visible ": true,
36
+ "closed": false,
37
+ "archived": false,
38
+ "views": 0,
39
+ "like_count": 0,
40
+ "starred": false,
41
+ "has_summary": false,
42
+ "archetype": "regular",
43
+ "last_poster_username": "fuzzy",
44
+ "category_id": 2,
45
+ "posters": [
46
+ {
47
+ "extras": "latest",
48
+ "description": "Original Poster, Most Recent Poster",
49
+ "user_id": 3
50
+ }
51
+ ]
52
+ },
53
+ {
54
+ "id": 4,
55
+ "title": "This is a markdown post",
56
+ "fancy_title": "This is a markdown post",
57
+ "slug": "this-is-a-markdown-post",
58
+ "posts_count": 2,
59
+ "reply_count": 0,
60
+ "highest_post_number": 2,
61
+ "image_url": null,
62
+ "created_at": "2014-04-16T13:52:58.777-04:00",
63
+ "last_posted_at": "2014-04-16T14:31:37.371-04:00",
64
+ "bumped": true,
65
+ "bumped_at": "2014-04-16T14:31:37.371-04:00",
66
+ "unseen": false,
67
+ "last_read_post_number": 2,
68
+ "unread": 0,
69
+ "new_posts": 0,
70
+ "pinned": false,
71
+ "unpinned ": null,
72
+ "visible": true,
73
+ "closed": false,
74
+ "archived": false,
75
+ "views": 0,
76
+ "like_count": 0,
77
+ "starred": false,
78
+ "has_summary": false,
79
+ "archetype": "regular",
80
+ "last_poster_username": "fuzzy",
81
+ "category_id": 2,
82
+ "posters": [
83
+ {
84
+ "extras": "latest",
85
+ "description": "Original Poster, Most Recent Poster",
86
+ "user_id": 3
87
+ }
88
+ ]
89
+ }
90
+ ]
91
+ }
92
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "api_key": {
3
+ "id": 10,
4
+ "key": "c999afe41d3a6ed0798a13ef69f90c4cc7d38120818c4a098e402bc5e5ef2085",
5
+ "user": null
6
+ }
7
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "api_key": {
3
+ "id": 10,
4
+ "key": "d8a80230fd14531c9b4809c7800b1fe78073a3771d3d2a56a5671bace55bc393",
5
+ "user": null
6
+ }
7
+ }
@@ -0,0 +1,37 @@
1
+ {
2
+ "admin_user": {
3
+ "id": 11,
4
+ "username": "dave4",
5
+ "uploaded_avatar_id": null,
6
+ "avatar_template": "/letter_avatar/dave4/{size}/2.png",
7
+ "active": true,
8
+ "admin": true,
9
+ "moderator": false,
10
+ "last_seen_at": null,
11
+ "last_emailed_at": "2014-12-31T17:11:51.189Z",
12
+ "created_at": "2014-12-08T14:32:29.906Z",
13
+ "last_seen_age": null,
14
+ "last_emailed_age": "20d",
15
+ "created_at_age": "43d",
16
+ "username_lower": "dave4",
17
+ "trust_level": 0,
18
+ "trust_level_locked": false,
19
+ "flag_level": 0,
20
+ "title": null,
21
+ "suspended_at": null,
22
+ "suspended_till": null,
23
+ "suspended": null,
24
+ "blocked": false,
25
+ "time_read": "< 1m",
26
+ "days_visited": 0,
27
+ "posts_read_count": 0,
28
+ "topics_entered": 0,
29
+ "post_count": 0,
30
+ "can_send_activation_email": true,
31
+ "can_activate": false,
32
+ "can_deactivate": false,
33
+ "ip_address": "127.0.0.1",
34
+ "registration_ip_address": "127.0.0.1",
35
+ "single_sign_on_record": null
36
+ }
37
+ }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: discourse_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Saffron
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2015-01-15 00:00:00.000000000 Z
13
+ date: 2015-04-14 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: faraday
@@ -208,7 +208,7 @@ files:
208
208
  - examples/topic_lists.rb
209
209
  - examples/update_user.rb
210
210
  - lib/discourse_api.rb
211
- - lib/discourse_api/api/api_admin.rb
211
+ - lib/discourse_api/api/api_key.rb
212
212
  - lib/discourse_api/api/backups.rb
213
213
  - lib/discourse_api/api/badges.rb
214
214
  - lib/discourse_api/api/categories.rb
@@ -228,7 +228,7 @@ files:
228
228
  - lib/discourse_api/single_sign_on.rb
229
229
  - lib/discourse_api/version.rb
230
230
  - routes.txt
231
- - spec/discourse_api/api/api_admin_spec.rb
231
+ - spec/discourse_api/api/api_key_spec.rb
232
232
  - spec/discourse_api/api/backups_spec.rb
233
233
  - spec/discourse_api/api/badges_spec.rb
234
234
  - spec/discourse_api/api/categories_spec.rb
@@ -247,9 +247,11 @@ files:
247
247
  - spec/fixtures/badges.json
248
248
  - spec/fixtures/categories.json
249
249
  - spec/fixtures/category_latest_topics.json
250
+ - spec/fixtures/category_topics.json
250
251
  - spec/fixtures/email_list_all.json
251
252
  - spec/fixtures/email_settings.json
252
253
  - spec/fixtures/generate_api_key.json
254
+ - spec/fixtures/generate_master_key.json
253
255
  - spec/fixtures/groups.json
254
256
  - spec/fixtures/hot.json
255
257
  - spec/fixtures/latest.json
@@ -257,6 +259,7 @@ files:
257
259
  - spec/fixtures/notifications.json
258
260
  - spec/fixtures/post.json
259
261
  - spec/fixtures/private_messages.json
262
+ - spec/fixtures/regenerate_api_key.json
260
263
  - spec/fixtures/search.json
261
264
  - spec/fixtures/topic.json
262
265
  - spec/fixtures/topic_invite_user.json
@@ -266,6 +269,7 @@ files:
266
269
  - spec/fixtures/user_activate_success.json
267
270
  - spec/fixtures/user_badges.json
268
271
  - spec/fixtures/user_create_success.json
272
+ - spec/fixtures/user_grant_admin.json
269
273
  - spec/fixtures/user_list.json
270
274
  - spec/fixtures/user_log_out_success.json
271
275
  - spec/fixtures/user_update_avatar_success.json
@@ -297,7 +301,7 @@ signing_key:
297
301
  specification_version: 4
298
302
  summary: Allows access to the Discourse API
299
303
  test_files:
300
- - spec/discourse_api/api/api_admin_spec.rb
304
+ - spec/discourse_api/api/api_key_spec.rb
301
305
  - spec/discourse_api/api/backups_spec.rb
302
306
  - spec/discourse_api/api/badges_spec.rb
303
307
  - spec/discourse_api/api/categories_spec.rb
@@ -316,9 +320,11 @@ test_files:
316
320
  - spec/fixtures/badges.json
317
321
  - spec/fixtures/categories.json
318
322
  - spec/fixtures/category_latest_topics.json
323
+ - spec/fixtures/category_topics.json
319
324
  - spec/fixtures/email_list_all.json
320
325
  - spec/fixtures/email_settings.json
321
326
  - spec/fixtures/generate_api_key.json
327
+ - spec/fixtures/generate_master_key.json
322
328
  - spec/fixtures/groups.json
323
329
  - spec/fixtures/hot.json
324
330
  - spec/fixtures/latest.json
@@ -326,6 +332,7 @@ test_files:
326
332
  - spec/fixtures/notifications.json
327
333
  - spec/fixtures/post.json
328
334
  - spec/fixtures/private_messages.json
335
+ - spec/fixtures/regenerate_api_key.json
329
336
  - spec/fixtures/search.json
330
337
  - spec/fixtures/topic.json
331
338
  - spec/fixtures/topic_invite_user.json
@@ -335,6 +342,7 @@ test_files:
335
342
  - spec/fixtures/user_activate_success.json
336
343
  - spec/fixtures/user_badges.json
337
344
  - spec/fixtures/user_create_success.json
345
+ - spec/fixtures/user_grant_admin.json
338
346
  - spec/fixtures/user_list.json
339
347
  - spec/fixtures/user_log_out_success.json
340
348
  - spec/fixtures/user_update_avatar_success.json
@@ -1,18 +0,0 @@
1
- module DiscourseApi
2
- module API
3
- module ApiAdmin
4
- def api
5
- response = get("/admin/api.json")
6
- response.body
7
- end
8
-
9
- def generate_api_key(user_id)
10
- response = post("/admin/users/#{user_id}/generate_api_key.json")
11
- end
12
-
13
- def revoke_api_key(user_id)
14
- response = delete("/admin/users/#{user_id}/revoke_api_key.json")
15
- end
16
- end
17
- end
18
- end
@@ -1,63 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe DiscourseApi::API::ApiAdmin do
4
- subject {
5
- DiscourseApi::Client.new(
6
- "http://localhost:3000",
7
- "test_d7fd0429940",
8
- "test_user"
9
- )
10
- }
11
-
12
- describe "#api" do
13
- before do
14
- url = "http://localhost:3000/admin/api.json?" +
15
- "api_key=test_d7fd0429940&api_username=test_user"
16
- stub_get(url).to_return(body: fixture("api.json"),
17
- headers: { content_type: "application/json" })
18
- end
19
-
20
- it "requests the correct resource" do
21
- subject.api
22
- url = "http://localhost:3000/admin/api.json?" +
23
- "api_key=test_d7fd0429940&api_username=test_user"
24
- expect(a_get(url)).to have_been_made
25
- end
26
-
27
- it "returns the requested api keys" do
28
- api = subject.api
29
- expect(api).to be_an Array
30
- expect(api.first).to be_a Hash
31
- expect(api.first).to have_key('key')
32
- end
33
- end
34
-
35
- describe "#generate_api_key" do
36
- before do
37
- url = "http://localhost:3000/admin/users/2/generate_api_key.json?" +
38
- "api_key=test_d7fd0429940&api_username=test_user"
39
- stub_post(url).to_return(body: fixture("generate_api_key.json"),
40
- headers: { content_type: "application/json" })
41
- end
42
-
43
- it "returns the generated api key" do
44
- api_key = subject.generate_api_key(2)
45
- expect(api_key).to be_a Hash
46
- expect(api_key['api_key']).to have_key('key')
47
- end
48
- end
49
-
50
- describe "#revoke_api_key" do
51
- before do
52
- url = "http://localhost:3000/admin/users/2/revoke_api_key.json?" +
53
- "api_key=test_d7fd0429940&api_username=test_user"
54
- stub_post(url).to_return(body: "",
55
- headers: { content_type: "application/json" })
56
- end
57
-
58
- it "returns 200" do
59
- response = subject.revoke_api_key(2)
60
- expect(response['status']).to eq(200)
61
- end
62
- end
63
- end