Skip to content

Commit e41ed75

Browse files
add integration test
Signed-off-by: Sebastian Webber <[email protected]>
1 parent cdd03e7 commit e41ed75

File tree

1 file changed

+105
-68
lines changed

1 file changed

+105
-68
lines changed

tests/ruby/auth_query_spec.rb

+105-68
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
require_relative 'spec_helper'
44
require_relative 'helpers/auth_query_helper'
55

6-
describe "Auth Query" do
7-
let(:configured_instances) {[5432, 10432]}
6+
describe 'Auth Query' do
7+
let(:configured_instances) { [5432, 10_432] }
88
let(:config_user) { { 'username' => 'sharding_user', 'password' => 'sharding_user' } }
99
let(:pg_user) { { 'username' => 'sharding_user', 'password' => 'sharding_user' } }
10-
let(:processes) { Helpers::AuthQuery.single_shard_auth_query(pool_name: "sharded_db", pg_user: pg_user, config_user: config_user, extra_conf: config, wait_until_ready: wait_until_ready ) }
10+
let(:processes) do
11+
Helpers::AuthQuery.single_shard_auth_query(pool_name: 'sharded_db', pg_user: pg_user, config_user: config_user,
12+
extra_conf: config, wait_until_ready: wait_until_ready)
13+
end
1114
let(:config) { {} }
1215
let(:wait_until_ready) { true }
1316

@@ -19,119 +22,150 @@
1922
@failing_process = false
2023
end
2124

22-
context "when auth_query is not configured" do
25+
context 'when auth_query is not configured' do
2326
context 'and cleartext passwords are set' do
24-
it "uses local passwords" do
25-
conn = PG.connect(processes.pgcat.connection_string("sharded_db", config_user['username'], config_user['password']))
27+
it 'uses local passwords' do
28+
conn = PG.connect(processes.pgcat.connection_string('sharded_db', config_user['username'],
29+
config_user['password']))
2630

27-
expect(conn.async_exec("SELECT 1 + 2")).not_to be_nil
31+
expect(conn.async_exec('SELECT 1 + 2')).not_to be_nil
2832
end
2933
end
3034

3135
context 'and cleartext passwords are not set' do
3236
let(:config_user) { { 'username' => 'sharding_user' } }
3337

34-
it "does not start because it is not possible to authenticate" do
38+
it 'does not start because it is not possible to authenticate' do
3539
@failing_process = true
36-
expect { processes.pgcat }.to raise_error(StandardError, /You have to specify a user password for every pool if auth_query is not specified/)
40+
expect do
41+
processes.pgcat
42+
end.to raise_error(StandardError,
43+
/You have to specify a user password for every pool if auth_query is not specified/)
3744
end
3845
end
3946
end
4047

4148
context 'when auth_query is configured' do
4249
context 'with global configuration' do
4350
around(:example) do |example|
44-
4551
# Set up auth query
4652
Helpers::AuthQuery.set_up_auth_query_for_user(
4753
user: 'md5_auth_user',
4854
password: 'secret'
49-
);
55+
)
5056

5157
example.run
5258

5359
# Drop auth query support
5460
Helpers::AuthQuery.tear_down_auth_query_for_user(
5561
user: 'md5_auth_user',
5662
password: 'secret'
57-
);
63+
)
64+
end
65+
66+
context 'with different lookup database' do
67+
let(:config) do
68+
{ 'general' => {
69+
'auth_query' => "SELECT * FROM public.user_lookup2('$1');",
70+
'auth_query_user' => 'md5_auth_use2r',
71+
'auth_query_password' => 'secret',
72+
'auth_query_database' => 'lookup_db' ## doesn't exist yet
73+
} }
74+
end
75+
76+
it 'it uses obtained passwords' do ## should fail
77+
connection_string = processes.pgcat.connection_string('sharded_db', pg_user['username'])
78+
conn = PG.connect(connection_string)
79+
80+
expect(conn.exec('SELECT 1 + 2')).not_to be_nil
81+
end
5882
end
5983

6084
context 'with correct global parameters' do
61-
let(:config) { { 'general' => { 'auth_query' => "SELECT * FROM public.user_lookup('$1');", 'auth_query_user' => 'md5_auth_user', 'auth_query_password' => 'secret' } } }
85+
let(:config) do
86+
{ 'general' => { 'auth_query' => "SELECT * FROM public.user_lookup('$1');", 'auth_query_user' => 'md5_auth_user',
87+
'auth_query_password' => 'secret' } }
88+
end
6289
context 'and with cleartext passwords set' do
6390
it 'it uses local passwords' do
64-
conn = PG.connect(processes.pgcat.connection_string("sharded_db", pg_user['username'], pg_user['password']))
65-
expect(conn.exec("SELECT 1 + 2")).not_to be_nil
91+
conn = PG.connect(processes.pgcat.connection_string('sharded_db', pg_user['username'], pg_user['password']))
92+
expect(conn.exec('SELECT 1 + 2')).not_to be_nil
6693
end
6794
end
6895

6996
context 'and with cleartext passwords not set' do
7097
let(:config_user) { { 'username' => 'sharding_user', 'password' => 'sharding_user' } }
7198

7299
it 'it uses obtained passwords' do
73-
connection_string = processes.pgcat.connection_string("sharded_db", pg_user['username'], pg_user['password'])
100+
connection_string = processes.pgcat.connection_string('sharded_db', pg_user['username'],
101+
pg_user['password'])
74102
conn = PG.connect(connection_string)
75-
expect(conn.async_exec("SELECT 1 + 2")).not_to be_nil
103+
expect(conn.async_exec('SELECT 1 + 2')).not_to be_nil
76104
end
77105

78106
it 'allows passwords to be changed without closing existing connections' do
79-
pgconn = PG.connect(processes.pgcat.connection_string("sharded_db", pg_user['username']))
80-
expect(pgconn.exec("SELECT 1 + 2")).not_to be_nil
107+
pgconn = PG.connect(processes.pgcat.connection_string('sharded_db', pg_user['username']))
108+
expect(pgconn.exec('SELECT 1 + 2')).not_to be_nil
81109
Helpers::AuthQuery.exec_in_instances(query: "ALTER USER #{pg_user['username']} WITH ENCRYPTED PASSWORD 'secret2';")
82-
expect(pgconn.exec("SELECT 1 + 4")).not_to be_nil
110+
expect(pgconn.exec('SELECT 1 + 4')).not_to be_nil
83111
Helpers::AuthQuery.exec_in_instances(query: "ALTER USER #{pg_user['username']} WITH ENCRYPTED PASSWORD '#{pg_user['password']}';")
84112
end
85113

86114
it 'allows passwords to be changed and that new password is needed when reconnecting' do
87-
pgconn = PG.connect(processes.pgcat.connection_string("sharded_db", pg_user['username']))
88-
expect(pgconn.exec("SELECT 1 + 2")).not_to be_nil
115+
pgconn = PG.connect(processes.pgcat.connection_string('sharded_db', pg_user['username']))
116+
expect(pgconn.exec('SELECT 1 + 2')).not_to be_nil
89117
Helpers::AuthQuery.exec_in_instances(query: "ALTER USER #{pg_user['username']} WITH ENCRYPTED PASSWORD 'secret2';")
90-
newconn = PG.connect(processes.pgcat.connection_string("sharded_db", pg_user['username'], 'secret2'))
91-
expect(newconn.exec("SELECT 1 + 2")).not_to be_nil
118+
newconn = PG.connect(processes.pgcat.connection_string('sharded_db', pg_user['username'], 'secret2'))
119+
expect(newconn.exec('SELECT 1 + 2')).not_to be_nil
92120
Helpers::AuthQuery.exec_in_instances(query: "ALTER USER #{pg_user['username']} WITH ENCRYPTED PASSWORD '#{pg_user['password']}';")
93121
end
94122
end
95123
end
96124

97125
context 'with wrong parameters' do
98-
let(:config) { { 'general' => { 'auth_query' => 'SELECT 1', 'auth_query_user' => 'wrong_user', 'auth_query_password' => 'wrong' } } }
126+
let(:config) do
127+
{ 'general' => { 'auth_query' => 'SELECT 1', 'auth_query_user' => 'wrong_user',
128+
'auth_query_password' => 'wrong' } }
129+
end
99130

100131
context 'and with clear text passwords set' do
101-
it "it uses local passwords" do
102-
conn = PG.connect(processes.pgcat.connection_string("sharded_db", pg_user['username'], pg_user['password']))
132+
it 'it uses local passwords' do
133+
conn = PG.connect(processes.pgcat.connection_string('sharded_db', pg_user['username'], pg_user['password']))
103134

104-
expect(conn.async_exec("SELECT 1 + 2")).not_to be_nil
135+
expect(conn.async_exec('SELECT 1 + 2')).not_to be_nil
105136
end
106137
end
107138

108139
context 'and with cleartext passwords not set' do
109140
let(:config_user) { { 'username' => 'sharding_user' } }
110-
it "it fails to start as it cannot authenticate against servers" do
141+
it 'it fails to start as it cannot authenticate against servers' do
111142
@failing_process = true
112-
expect { PG.connect(processes.pgcat.connection_string("sharded_db", pg_user['username'], pg_user['password'])) }.to raise_error(StandardError, /Error trying to obtain password from auth_query/ )
143+
expect do
144+
PG.connect(processes.pgcat.connection_string('sharded_db', pg_user['username'],
145+
pg_user['password']))
146+
end.to raise_error(StandardError, /Error trying to obtain password from auth_query/)
113147
end
114148

115149
context 'and we fix the issue and reload' do
116150
let(:wait_until_ready) { false }
117151

118152
it 'fails in the beginning but starts working after reloading config' do
119-
connection_string = processes.pgcat.connection_string("sharded_db", pg_user['username'], pg_user['password'])
120-
while !(processes.pgcat.logs =~ /Waiting for clients/) do
121-
sleep 0.5
122-
end
153+
connection_string = processes.pgcat.connection_string('sharded_db', pg_user['username'],
154+
pg_user['password'])
155+
sleep 0.5 until processes.pgcat.logs =~ /Waiting for clients/
123156

124-
expect { PG.connect(connection_string)}.to raise_error(PG::ConnectionBad)
157+
expect { PG.connect(connection_string) }.to raise_error(PG::ConnectionBad)
125158
expect(processes.pgcat.logs).to match(/Error trying to obtain password from auth_query/)
126159

127160
current_config = processes.pgcat.current_config
128-
config = { 'general' => { 'auth_query' => "SELECT * FROM public.user_lookup('$1');", 'auth_query_user' => 'md5_auth_user', 'auth_query_password' => 'secret' } }
161+
config = { 'general' => { 'auth_query' => "SELECT * FROM public.user_lookup('$1');",
162+
'auth_query_user' => 'md5_auth_user', 'auth_query_password' => 'secret' } }
129163
processes.pgcat.update_config(current_config.deep_merge(config))
130164
processes.pgcat.reload_config
131165

132166
conn = nil
133-
expect { conn = PG.connect(connection_string)}.not_to raise_error
134-
expect(conn.async_exec("SELECT 1 + 2")).not_to be_nil
167+
expect { conn = PG.connect(connection_string) }.not_to raise_error
168+
expect(conn.async_exec('SELECT 1 + 2')).not_to be_nil
135169
end
136170
end
137171
end
@@ -140,75 +174,78 @@
140174

141175
context 'with per pool configuration' do
142176
around(:example) do |example|
143-
144177
# Set up auth query
145178
Helpers::AuthQuery.set_up_auth_query_for_user(
146179
user: 'md5_auth_user',
147180
password: 'secret'
148-
);
181+
)
149182

150183
Helpers::AuthQuery.set_up_auth_query_for_user(
151184
user: 'md5_auth_user1',
152185
password: 'secret',
153186
database: 'shard1'
154-
);
187+
)
155188

156189
example.run
157190

158191
# Tear down auth query
159192
Helpers::AuthQuery.tear_down_auth_query_for_user(
160193
user: 'md5_auth_user',
161194
password: 'secret'
162-
);
195+
)
163196

164197
Helpers::AuthQuery.tear_down_auth_query_for_user(
165198
user: 'md5_auth_user1',
166199
password: 'secret',
167200
database: 'shard1'
168-
);
201+
)
169202
end
170203

171204
context 'with correct parameters' do
172-
let(:processes) { Helpers::AuthQuery.two_pools_auth_query(pool_names: ["sharded_db0", "sharded_db1"], pg_user: pg_user, config_user: config_user, extra_conf: config ) }
173-
let(:config) {
174-
{ 'pools' =>
175-
{
176-
'sharded_db0' => {
177-
'auth_query' => "SELECT * FROM public.user_lookup('$1');",
178-
'auth_query_user' => 'md5_auth_user',
179-
'auth_query_password' => 'secret'
180-
},
181-
'sharded_db1' => {
182-
'auth_query' => "SELECT * FROM public.user_lookup('$1');",
183-
'auth_query_user' => 'md5_auth_user1',
184-
'auth_query_password' => 'secret'
185-
},
205+
let(:processes) do
206+
Helpers::AuthQuery.two_pools_auth_query(pool_names: %w[sharded_db0 sharded_db1], pg_user: pg_user,
207+
config_user: config_user, extra_conf: config)
208+
end
209+
let(:config) do
210+
{ 'pools' => {
211+
'sharded_db0' => {
212+
'auth_query' => "SELECT * FROM public.user_lookup('$1');",
213+
'auth_query_user' => 'md5_auth_user',
214+
'auth_query_password' => 'secret'
215+
},
216+
'sharded_db1' => {
217+
'auth_query' => "SELECT * FROM public.user_lookup('$1');",
218+
'auth_query_user' => 'md5_auth_user1',
219+
'auth_query_password' => 'secret'
186220
}
187-
}
188-
}
221+
} }
222+
end
189223

190224
context 'and with cleartext passwords set' do
191225
it 'it uses local passwords' do
192-
conn = PG.connect(processes.pgcat.connection_string("sharded_db0", pg_user['username'], pg_user['password']))
193-
expect(conn.exec("SELECT 1 + 2")).not_to be_nil
194-
conn = PG.connect(processes.pgcat.connection_string("sharded_db1", pg_user['username'], pg_user['password']))
195-
expect(conn.exec("SELECT 1 + 2")).not_to be_nil
226+
conn = PG.connect(processes.pgcat.connection_string('sharded_db0', pg_user['username'],
227+
pg_user['password']))
228+
expect(conn.exec('SELECT 1 + 2')).not_to be_nil
229+
conn = PG.connect(processes.pgcat.connection_string('sharded_db1', pg_user['username'],
230+
pg_user['password']))
231+
expect(conn.exec('SELECT 1 + 2')).not_to be_nil
196232
end
197233
end
198234

199235
context 'and with cleartext passwords not set' do
200236
let(:config_user) { { 'username' => 'sharding_user' } }
201237

202238
it 'it uses obtained passwords' do
203-
connection_string = processes.pgcat.connection_string("sharded_db0", pg_user['username'], pg_user['password'])
239+
connection_string = processes.pgcat.connection_string('sharded_db0', pg_user['username'],
240+
pg_user['password'])
204241
conn = PG.connect(connection_string)
205-
expect(conn.async_exec("SELECT 1 + 2")).not_to be_nil
206-
connection_string = processes.pgcat.connection_string("sharded_db1", pg_user['username'], pg_user['password'])
242+
expect(conn.async_exec('SELECT 1 + 2')).not_to be_nil
243+
connection_string = processes.pgcat.connection_string('sharded_db1', pg_user['username'],
244+
pg_user['password'])
207245
conn = PG.connect(connection_string)
208-
expect(conn.async_exec("SELECT 1 + 2")).not_to be_nil
246+
expect(conn.async_exec('SELECT 1 + 2')).not_to be_nil
209247
end
210248
end
211-
212249
end
213250
end
214251
end

0 commit comments

Comments
 (0)