|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | 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 } |
12 | 7 |
|
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 |
20 | 14 |
|
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 |
28 | 21 |
|
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 |
35 | 28 |
|
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') |
41 | 33 |
|
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 |
46 | 38 |
|
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 |
54 | 46 | end |
55 | 47 |
|
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 |
58 | 142 | end |
59 | 143 | end |
0 commit comments