Skip to content

Commit 1519c96

Browse files
authored
Add API keys methods (#279)
* Add API keys methods * Fix linter errors
1 parent d0f7fc9 commit 1519c96

File tree

3 files changed

+165
-51
lines changed

3 files changed

+165
-51
lines changed

lib/meilisearch/client.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,26 @@ def fetch_raw_index(index_uid)
5555
def keys
5656
http_get '/keys'
5757
end
58-
alias get_keys keys
58+
59+
def key(key_uid)
60+
http_get "/keys/#{key_uid}"
61+
end
62+
63+
def create_key(key_options)
64+
body = Utils.transform_attributes(key_options)
65+
66+
http_post '/keys', body
67+
end
68+
69+
def update_key(key_uid, key_options)
70+
body = Utils.transform_attributes(key_options)
71+
72+
http_patch "/keys/#{key_uid}", body
73+
end
74+
75+
def delete_key(key_uid)
76+
http_delete "/keys/#{key_uid}"
77+
end
5978

6079
### HEALTH
6180

lib/meilisearch/http_request.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ def http_put(relative_path = '', body = nil, query_params = nil)
4949
)
5050
end
5151

52+
def http_patch(relative_path = '', body = nil, query_params = nil)
53+
send_request(
54+
proc { |path, config| self.class.patch(path, config) },
55+
relative_path,
56+
query_params: query_params,
57+
body: body,
58+
options: @options
59+
)
60+
end
61+
5262
def http_delete(relative_path = '')
5363
send_request(
5464
proc { |path, config| self.class.delete(path, config) },
@@ -60,10 +70,11 @@ def http_delete(relative_path = '')
6070
private
6171

6272
def build_default_options_headers(api_key = nil)
63-
{
64-
'Content-Type' => 'application/json',
65-
'Authorization' => "Bearer #{api_key}"
66-
}.compact
73+
header = {
74+
'Content-Type' => 'application/json'
75+
}
76+
header = header.merge('Authorization' => "Bearer #{api_key}") unless api_key.nil?
77+
header
6778
end
6879

6980
def merge_options(default_options, added_options = {})
Lines changed: 130 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,143 @@
11
# frozen_string_literal: true
22

33
RSpec.describe 'MeiliSearch::Client - Keys' do
4-
it 'gets the list of keys' do
5-
response = client.keys
6-
expect(response).to be_a(Hash)
7-
expect(response.count).to eq(2)
8-
expect(response.keys).to contain_exactly('private', 'public')
9-
expect(response['private']).to be_a(String)
10-
expect(response['public']).to be_a(String)
11-
end
4+
context 'Test the default key roles' do
5+
let(:public_key) { client.keys.filter { |k| k['description'].start_with? 'Default Search API Key' }.first }
6+
let(:private_key) { client.keys.filter { |k| k['description'].start_with? 'Default Admin API Key' }.first }
127

13-
it 'fails to get settings if public key used' do
14-
public_key = client.keys['public']
15-
new_client = MeiliSearch::Client.new(URL, public_key)
16-
expect do
17-
new_client.index(random_uid).settings
18-
end.to raise_meilisearch_api_error_with(403, 'invalid_api_key', 'auth')
19-
end
8+
it 'fails to get settings if public key used' do
9+
new_client = MeiliSearch::Client.new(URL, public_key['key'])
10+
expect do
11+
new_client.index(random_uid).settings
12+
end.to raise_meilisearch_api_error_with(403, 'invalid_api_key', 'auth')
13+
end
2014

21-
it 'fails to get keys if private key used' do
22-
private_key = client.keys['private']
23-
new_client = MeiliSearch::Client.new(URL, private_key)
24-
expect do
25-
new_client.keys
26-
end.to raise_meilisearch_api_error_with(403, 'invalid_api_key', 'auth')
27-
end
15+
it 'fails to get keys if private key used' do
16+
new_client = MeiliSearch::Client.new(URL, private_key['key'])
17+
expect do
18+
new_client.keys
19+
end.to raise_meilisearch_api_error_with(403, 'invalid_api_key', 'auth')
20+
end
2821

29-
it 'fails to search if no key used' do
30-
new_client = MeiliSearch::Client.new(URL)
31-
expect do
32-
new_client.index(random_uid).settings
33-
end.to raise_meilisearch_api_error_with(401, 'missing_authorization_header', 'auth')
34-
end
22+
it 'fails to search if no key used' do
23+
new_client = MeiliSearch::Client.new(URL)
24+
expect do
25+
new_client.index(random_uid).settings
26+
end.to raise_meilisearch_api_error_with(401, 'missing_authorization_header', 'auth')
27+
end
3528

36-
it 'succeeds to search when using public key' do
37-
uid = random_uid
38-
public_key = client.keys['public']
39-
index = client.index(uid)
40-
index.add_documents!(title: 'Test')
29+
it 'succeeds to search when using public key' do
30+
uid = random_uid
31+
index = client.index(uid)
32+
index.add_documents!(title: 'Test')
4133

42-
new_client = MeiliSearch::Client.new(URL, public_key)
43-
response = new_client.index(uid).search('test')
44-
expect(response).to have_key('hits')
45-
end
34+
new_client = MeiliSearch::Client.new(URL, public_key['key'])
35+
response = new_client.index(uid).search('test')
36+
expect(response).to have_key('hits')
37+
end
4638

47-
it 'succeeds to get settings when using private key' do
48-
uid = random_uid
49-
client.create_index!(uid)
50-
private_key = client.keys['private']
51-
new_client = MeiliSearch::Client.new(URL, private_key)
52-
response = new_client.index(uid).settings
53-
expect(response).to have_key('rankingRules')
39+
it 'succeeds to get settings when using private key' do
40+
uid = random_uid
41+
client.create_index!(uid)
42+
new_client = MeiliSearch::Client.new(URL, private_key['key'])
43+
response = new_client.index(uid).settings
44+
expect(response).to have_key('rankingRules')
45+
end
5446
end
5547

56-
it 'works with method aliases' do
57-
expect(client.method(:keys) == client.method(:get_keys)).to be_truthy
48+
context 'Test the key managements' do
49+
it 'gets the list of the default keys' do
50+
response = client.keys
51+
expect(response).to be_a(Array)
52+
expect(response.count).to be >= 2
53+
end
54+
55+
it 'creates a key' do
56+
key_options = {
57+
description: 'A new key to add docs',
58+
actions: ['documents.add'],
59+
indexes: ['*'],
60+
expiresAt: nil
61+
}
62+
new_key = client.create_key(key_options)
63+
expect(new_key['expiresAt']).to be_nil
64+
expect(new_key['key']).to be_a(String)
65+
expect(new_key['createdAt']).to be_a(String)
66+
expect(new_key['updatedAt']).to be_a(String)
67+
expect(new_key['indexes']).to eq(['*'])
68+
expect(new_key['description']).to eq('A new key to add docs')
69+
end
70+
71+
it 'creates a key using snake_case' do
72+
key_options = {
73+
description: 'A new key to add docs',
74+
actions: ['documents.add'],
75+
indexes: ['*'],
76+
expires_at: nil
77+
}
78+
new_key = client.create_key(key_options)
79+
expect(new_key['expiresAt']).to be_nil
80+
expect(new_key['key']).to be_a(String)
81+
expect(new_key['createdAt']).to be_a(String)
82+
expect(new_key['updatedAt']).to be_a(String)
83+
expect(new_key['indexes']).to eq(['*'])
84+
expect(new_key['description']).to eq('A new key to add docs')
85+
end
86+
87+
it 'gets a key' do
88+
key_options = {
89+
description: 'A new key to delete docs',
90+
actions: ['documents.delete'],
91+
indexes: ['*'],
92+
expiresAt: nil
93+
}
94+
new_key = client.create_key(key_options)
95+
expect(client.key(new_key['key'])['description']).to eq('A new key to delete docs')
96+
end
97+
98+
it 'update a key' do
99+
key_options = {
100+
description: 'A new key to delete docs',
101+
actions: ['documents.delete'],
102+
indexes: ['*'],
103+
expiresAt: nil
104+
}
105+
new_key = client.create_key(key_options)
106+
107+
new_updated_key = client.update_key(new_key['key'], indexes: ['coco'])
108+
109+
expect(new_updated_key['key']).to eq(new_key['key'])
110+
expect(new_updated_key['description']).to eq(new_key['description'])
111+
expect(new_updated_key['indexes']).to eq(['coco'])
112+
end
113+
114+
it 'update a key using snake_case' do
115+
key_options = {
116+
description: 'A new key to delete docs',
117+
actions: ['documents.delete'],
118+
indexes: ['*'],
119+
expires_at: nil
120+
}
121+
new_key = client.create_key(key_options)
122+
123+
new_updated_key = client.update_key(new_key['key'], indexes: ['coco'])
124+
125+
expect(new_updated_key['key']).to eq(new_key['key'])
126+
expect(new_updated_key['description']).to eq(new_key['description'])
127+
expect(new_updated_key['indexes']).to eq(['coco'])
128+
end
129+
130+
it 'deletes a key' do
131+
key_options = {
132+
description: 'A new key to add docs',
133+
actions: ['documents.add'],
134+
indexes: ['*'],
135+
expiresAt: nil
136+
}
137+
new_key = client.create_key(key_options)
138+
139+
client.delete_key(new_key['key'])
140+
expect(client.keys.filter { |k| k['key'] == new_key['key'] }).to be_empty
141+
end
58142
end
59143
end

0 commit comments

Comments
 (0)