2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-06-11 12:35:45 +08:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe Admin::ApiController do
|
|
|
|
|
|
|
|
it "is a subclass of AdminController" do
|
|
|
|
expect(Admin::ApiController < Admin::AdminController).to eq(true)
|
|
|
|
end
|
|
|
|
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
2018-06-11 12:35:45 +08:00
|
|
|
|
2019-12-12 19:45:00 +08:00
|
|
|
fab!(:key1, refind: false) { Fabricate(:api_key, description: "my key") }
|
|
|
|
fab!(:key2, refind: false) { Fabricate(:api_key, user: admin) }
|
2021-11-09 18:18:23 +08:00
|
|
|
fab!(:key3, refind: false) { Fabricate(:api_key, user: admin) }
|
2019-11-05 22:10:23 +08:00
|
|
|
|
2018-09-12 00:02:06 +08:00
|
|
|
context "as an admin" do
|
|
|
|
before do
|
|
|
|
sign_in(admin)
|
2018-06-11 12:35:45 +08:00
|
|
|
end
|
|
|
|
|
2018-09-12 00:02:06 +08:00
|
|
|
describe '#index' do
|
|
|
|
it "succeeds" do
|
|
|
|
get "/admin/api/keys.json"
|
|
|
|
expect(response.status).to eq(200)
|
2021-11-09 18:18:23 +08:00
|
|
|
expect(response.parsed_body["keys"].length).to eq(3)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can paginate results" do
|
|
|
|
get "/admin/api/keys.json?offset=0&limit=2"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body["keys"].map { |x| x["id"] }).to contain_exactly(key3.id, key2.id)
|
|
|
|
|
|
|
|
get "/admin/api/keys.json?offset=1&limit=2"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body["keys"].map { |x| x["id"] }).to contain_exactly(key2.id, key1.id)
|
|
|
|
|
|
|
|
get "/admin/api/keys.json?offset=2&limit=2"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body["keys"].map { |x| x["id"] }).to contain_exactly(key1.id)
|
2018-09-12 00:02:06 +08:00
|
|
|
end
|
2018-06-11 12:35:45 +08:00
|
|
|
end
|
|
|
|
|
2019-11-05 22:10:23 +08:00
|
|
|
describe '#show' do
|
|
|
|
it "succeeds" do
|
|
|
|
get "/admin/api/keys/#{key1.id}.json"
|
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 23:04:12 +08:00
|
|
|
data = response.parsed_body["key"]
|
2019-11-05 22:10:23 +08:00
|
|
|
expect(data["id"]).to eq(key1.id)
|
2019-12-12 19:45:00 +08:00
|
|
|
expect(data["key"]).to eq(nil)
|
|
|
|
expect(data["truncated_key"]).to eq(key1.key[0..3])
|
2019-11-05 22:10:23 +08:00
|
|
|
expect(data["description"]).to eq("my key")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#update' do
|
|
|
|
it "allows updating the description" do
|
|
|
|
original_key = key1.key
|
|
|
|
|
|
|
|
put "/admin/api/keys/#{key1.id}.json", params: {
|
|
|
|
key: {
|
|
|
|
description: "my new description",
|
|
|
|
key: "overridekey"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
key1.reload
|
|
|
|
expect(key1.description).to eq("my new description")
|
|
|
|
expect(key1.key).to eq(original_key)
|
2018-06-11 12:35:45 +08:00
|
|
|
|
2019-11-05 22:10:23 +08:00
|
|
|
expect(UserHistory.last.action).to eq(UserHistory.actions[:api_key_update])
|
|
|
|
expect(UserHistory.last.subject).to eq(key1.truncated_key)
|
2018-09-12 00:02:06 +08:00
|
|
|
end
|
2018-06-11 12:35:45 +08:00
|
|
|
|
2019-11-05 22:10:23 +08:00
|
|
|
it "returns 400 for invalid payloads" do
|
|
|
|
put "/admin/api/keys/#{key1.id}.json", params: {
|
|
|
|
key: "string not a hash"
|
|
|
|
}
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
|
|
|
|
put "/admin/api/keys/#{key1.id}.json", params: {}
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#destroy" do
|
|
|
|
it "works" do
|
|
|
|
expect(ApiKey.exists?(key1.id)).to eq(true)
|
|
|
|
|
|
|
|
delete "/admin/api/keys/#{key1.id}.json"
|
|
|
|
|
2018-09-12 00:02:06 +08:00
|
|
|
expect(response.status).to eq(200)
|
2019-11-05 22:10:23 +08:00
|
|
|
expect(ApiKey.exists?(key1.id)).to eq(false)
|
2018-06-11 12:35:45 +08:00
|
|
|
|
2019-11-05 22:10:23 +08:00
|
|
|
expect(UserHistory.last.action).to eq(UserHistory.actions[:api_key_destroy])
|
|
|
|
expect(UserHistory.last.subject).to eq(key1.truncated_key)
|
2018-09-12 00:02:06 +08:00
|
|
|
end
|
2018-06-11 12:35:45 +08:00
|
|
|
end
|
|
|
|
|
2019-11-05 22:10:23 +08:00
|
|
|
describe "#create" do
|
|
|
|
it "can create a master key" do
|
|
|
|
post "/admin/api/keys.json", params: {
|
|
|
|
key: {
|
|
|
|
description: "master key description"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
2020-05-07 23:04:12 +08:00
|
|
|
data = response.parsed_body
|
2019-11-05 22:10:23 +08:00
|
|
|
|
|
|
|
expect(data['key']['description']).to eq("master key description")
|
|
|
|
expect(data['key']['user']).to eq(nil)
|
|
|
|
expect(data['key']['key']).to_not eq(nil)
|
|
|
|
expect(data['key']['last_used_at']).to eq(nil)
|
2018-09-12 00:02:06 +08:00
|
|
|
|
2019-11-05 22:10:23 +08:00
|
|
|
key = ApiKey.find(data['key']['id'])
|
|
|
|
expect(key.description).to eq("master key description")
|
|
|
|
expect(key.user).to eq(nil)
|
|
|
|
|
|
|
|
expect(UserHistory.last.action).to eq(UserHistory.actions[:api_key_create])
|
|
|
|
expect(UserHistory.last.subject).to eq(key.truncated_key)
|
2018-09-12 00:02:06 +08:00
|
|
|
end
|
|
|
|
|
2019-11-05 22:10:23 +08:00
|
|
|
it "can create a user-specific key" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
post "/admin/api/keys.json", params: {
|
|
|
|
key: {
|
|
|
|
description: "restricted key description",
|
|
|
|
username: user.username
|
|
|
|
}
|
|
|
|
}
|
2018-09-12 00:02:06 +08:00
|
|
|
expect(response.status).to eq(200)
|
2019-11-05 22:10:23 +08:00
|
|
|
|
2020-05-07 23:04:12 +08:00
|
|
|
data = response.parsed_body
|
2019-11-05 22:10:23 +08:00
|
|
|
|
|
|
|
expect(data['key']['description']).to eq("restricted key description")
|
|
|
|
expect(data['key']['user']['username']).to eq(user.username)
|
|
|
|
expect(data['key']['key']).to_not eq(nil)
|
|
|
|
expect(data['key']['last_used_at']).to eq(nil)
|
|
|
|
|
|
|
|
key = ApiKey.find(data['key']['id'])
|
|
|
|
expect(key.description).to eq("restricted key description")
|
|
|
|
expect(key.user.id).to eq(user.id)
|
|
|
|
|
|
|
|
expect(UserHistory.last.action).to eq(UserHistory.actions[:api_key_create])
|
|
|
|
expect(UserHistory.last.subject).to eq(key.truncated_key)
|
2018-09-12 00:02:06 +08:00
|
|
|
end
|
2020-07-17 02:51:24 +08:00
|
|
|
|
|
|
|
describe 'Scopes' do
|
|
|
|
it 'creates an scope with allowed parameters' do
|
|
|
|
post "/admin/api/keys.json", params: {
|
|
|
|
key: {
|
|
|
|
description: "master key description",
|
2021-02-18 01:42:44 +08:00
|
|
|
scopes: [{ scope_id: 'topics:write', topic_id: '55' }]
|
2020-07-17 02:51:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
data = response.parsed_body
|
|
|
|
scope = ApiKeyScope.find_by(api_key_id: data.dig('key', 'id'))
|
|
|
|
|
|
|
|
expect(scope.resource).to eq('topics')
|
|
|
|
expect(scope.action).to eq('write')
|
|
|
|
expect(scope.allowed_parameters['topic_id']).to contain_exactly('55')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows multiple parameters separated by a comma' do
|
|
|
|
post "/admin/api/keys.json", params: {
|
|
|
|
key: {
|
|
|
|
description: "master key description",
|
2021-02-18 01:42:44 +08:00
|
|
|
scopes: [{ scope_id: 'topics:write', topic_id: '55,33' }]
|
2020-07-17 02:51:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
data = response.parsed_body
|
|
|
|
scope = ApiKeyScope.find_by(api_key_id: data.dig('key', 'id'))
|
|
|
|
|
|
|
|
expect(scope.allowed_parameters['topic_id']).to contain_exactly('55', '33')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'ignores invalid parameters' do
|
|
|
|
post "/admin/api/keys.json", params: {
|
|
|
|
key: {
|
|
|
|
description: "master key description",
|
2021-02-18 01:42:44 +08:00
|
|
|
scopes: [{ scope_id: 'topics:write', fake_id: '55' }]
|
2020-07-17 02:51:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
data = response.parsed_body
|
|
|
|
scope = ApiKeyScope.find_by(api_key_id: data.dig('key', 'id'))
|
|
|
|
|
|
|
|
expect(scope.allowed_parameters['fake_id']).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'fails when the scope is invalid' do
|
|
|
|
post "/admin/api/keys.json", params: {
|
|
|
|
key: {
|
|
|
|
description: "master key description",
|
2021-02-18 01:42:44 +08:00
|
|
|
scopes: [{ scope_id: 'something:else' }]
|
2020-07-17 02:51:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
end
|
2018-06-11 12:35:45 +08:00
|
|
|
end
|
|
|
|
|
2019-11-05 22:10:23 +08:00
|
|
|
describe "#revoke and #undo_revoke" do
|
|
|
|
it "works correctly" do
|
|
|
|
post "/admin/api/keys/#{key1.id}/revoke.json"
|
|
|
|
expect(response.status).to eq 200
|
|
|
|
|
|
|
|
key1.reload
|
|
|
|
expect(key1.revoked_at).to_not eq(nil)
|
|
|
|
expect(UserHistory.last.action).to eq(UserHistory.actions[:api_key_update])
|
|
|
|
expect(UserHistory.last.subject).to eq(key1.truncated_key)
|
|
|
|
expect(UserHistory.last.details).to eq(I18n.t("staff_action_logs.api_key.revoked"))
|
|
|
|
|
|
|
|
post "/admin/api/keys/#{key1.id}/undo-revoke.json"
|
|
|
|
expect(response.status).to eq 200
|
|
|
|
|
|
|
|
key1.reload
|
|
|
|
expect(key1.revoked_at).to eq(nil)
|
|
|
|
expect(UserHistory.last.action).to eq(UserHistory.actions[:api_key_update])
|
|
|
|
expect(UserHistory.last.subject).to eq(key1.truncated_key)
|
|
|
|
expect(UserHistory.last.details).to eq(I18n.t("staff_action_logs.api_key.restored"))
|
|
|
|
end
|
2018-06-11 12:35:45 +08:00
|
|
|
end
|
2020-07-17 02:51:24 +08:00
|
|
|
|
|
|
|
describe '#scopes' do
|
|
|
|
it 'includes scopes' do
|
|
|
|
get '/admin/api/keys/scopes.json'
|
|
|
|
|
|
|
|
scopes = response.parsed_body['scopes']
|
|
|
|
|
2021-12-06 10:27:25 +08:00
|
|
|
expect(scopes.keys).to contain_exactly('topics', 'users', 'email', 'posts', 'uploads', 'global', 'badges')
|
2020-07-17 02:51:24 +08:00
|
|
|
end
|
|
|
|
end
|
2019-11-05 22:10:23 +08:00
|
|
|
end
|
2018-09-12 00:02:06 +08:00
|
|
|
|
2019-11-05 22:10:23 +08:00
|
|
|
context "as a moderator" do
|
|
|
|
before do
|
2018-09-12 00:02:06 +08:00
|
|
|
sign_in(Fabricate(:moderator))
|
|
|
|
end
|
|
|
|
|
2019-11-05 22:10:23 +08:00
|
|
|
it "doesn't allow access" do
|
|
|
|
get "/admin/api/keys.json"
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
|
|
|
|
get "/admin/api/key/#{key1.id}.json"
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
|
|
|
|
post "/admin/api/keys.json", params: {
|
|
|
|
key: {
|
|
|
|
description: "master key description"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
|
2021-11-09 18:18:23 +08:00
|
|
|
expect(ApiKey.count).to eq(3)
|
2019-11-05 22:10:23 +08:00
|
|
|
end
|
2018-06-11 12:35:45 +08:00
|
|
|
end
|
|
|
|
end
|