1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
# frozen_string_literal: true
# rubocop:todo all
require 'spec_helper'
describe Mongo::Auth::User do
let(:options) do
{ database: 'testing', user: 'user', password: 'pass' }
end
let(:user) do
described_class.new(options)
end
shared_examples_for 'sets database and auth source to admin' do
it 'sets database to admin' do
expect(user.database).to eq('admin')
end
it 'sets auth source to admin' do
expect(user.auth_source).to eq('admin')
end
end
shared_examples_for 'sets auth source to $external' do
it 'sets auth source to $external' do
expect(user.auth_source).to eq('$external')
end
end
describe '#initialize' do
let(:user) { Mongo::Auth::User.new(options) }
context 'no options' do
let(:options) { {} }
it 'succeeds' do
expect(user).to be_a(Mongo::Auth::User)
end
it_behaves_like 'sets database and auth source to admin'
end
context 'invalid mechanism' do
let(:options) { {auth_mech: :invalid} }
it 'raises ArgumentError' do
expect do
user
end.to raise_error(Mongo::Auth::InvalidMechanism, ":invalid is invalid, please use one of the following mechanisms: :aws, :gssapi, :mongodb_cr, :mongodb_x509, :plain, :scram, :scram256")
end
end
context 'mechanism given as string' do
let(:options) { {auth_mech: 'scram'} }
context 'not linting' do
require_no_linting
it 'warns' do
expect(Mongo::Logger.logger).to receive(:warn)
user
end
it 'converts mechanism to symbol' do
expect(user.mechanism).to eq(:scram)
end
it_behaves_like 'sets database and auth source to admin'
end
context 'linting' do
require_linting
it 'raises LintError' do
expect do
user
end.to raise_error(Mongo::Error::LintError, "Auth mechanism \"scram\" must be specified as a symbol")
end
end
end
context 'mechanism given as symbol' do
let(:options) { {auth_mech: :scram} }
it 'does not warn' do
expect(Mongo::Logger.logger).not_to receive(:warn)
user
end
it 'stores mechanism' do
expect(user.mechanism).to eq(:scram)
end
it_behaves_like 'sets database and auth source to admin'
end
context 'mechanism is x509' do
let(:options) { {auth_mech: :mongodb_x509} }
it 'sets database to admin' do
expect(user.database).to eq('admin')
end
it_behaves_like 'sets auth source to $external'
context 'database is explicitly given' do
let(:options) { {auth_mech: :mongodb_x509, database: 'foo'} }
it 'sets database to the specified one' do
expect(user.database).to eq('foo')
end
it_behaves_like 'sets auth source to $external'
end
end
it 'sets the database' do
expect(user.database).to eq('testing')
end
it 'sets the name' do
expect(user.name).to eq('user')
end
it 'sets the password' do
expect(user.password).to eq('pass')
end
end
describe '#auth_key' do
let(:nonce) do
end
let(:expected) do
Digest::MD5.hexdigest("#{nonce}#{user.name}#{user.hashed_password}")
end
it 'returns the users authentication key' do
expect(user.auth_key(nonce)).to eq(expected)
end
end
describe '#encoded_name' do
context 'when the user name contains an =' do
let(:options) do
{ user: 'user=' }
end
it 'escapes the = character to =3D' do
expect(user.encoded_name).to eq('user=3D')
end
it 'returns a UTF-8 string' do
expect(user.encoded_name.encoding.name).to eq('UTF-8')
end
end
context 'when the user name contains a ,' do
let(:options) do
{ user: 'user,' }
end
it 'escapes the , character to =2C' do
expect(user.encoded_name).to eq('user=2C')
end
it 'returns a UTF-8 string' do
expect(user.encoded_name.encoding.name).to eq('UTF-8')
end
end
context 'when the user name contains no special characters' do
it 'does not alter the user name' do
expect(user.name).to eq('user')
end
it 'returns a UTF-8 string' do
expect(user.encoded_name.encoding.name).to eq('UTF-8')
end
end
end
describe '#hashed_password' do
let(:expected) do
Digest::MD5.hexdigest("user:mongo:pass")
end
it 'returns the hashed password' do
expect(user.hashed_password).to eq(expected)
end
context 'password not given' do
let(:options) { {user: 'foo'} }
it 'raises MissingPassword' do
expect do
user.hashed_password
end.to raise_error(Mongo::Error::MissingPassword)
end
end
end
describe '#sasl_prepped_password' do
let(:expected) do
'pass'
end
it 'returns the clear text password' do
expect(user.send(:sasl_prepped_password)).to eq(expected)
end
it 'returns the password encoded in utf-8' do
expect(user.sasl_prepped_password.encoding.name).to eq('UTF-8')
end
context 'password not given' do
let(:options) { {user: 'foo'} }
it 'raises MissingPassword' do
expect do
user.sasl_prepped_password
end.to raise_error(Mongo::Error::MissingPassword)
end
end
end
describe '#mechanism' do
context 'when the option is provided' do
let(:options) do
{ database: 'testing', user: 'user', password: 'pass', auth_mech: :plain }
end
let(:user) do
described_class.new(options)
end
it 'returns the option' do
expect(user.mechanism).to eq(:plain)
end
end
context 'when no option is provided' do
let(:user) do
described_class.new(options)
end
it 'returns the default' do
expect(user.mechanism).to be_nil
end
end
end
describe '#auth_mech_properties' do
context 'when the option is provided' do
let(:auth_mech_properties) do
{ service_name: 'test',
service_realm: 'test',
canonicalize_host_name: true }
end
let(:options) do
{ database: 'testing', user: 'user', password: 'pass', auth_mech_properties: auth_mech_properties }
end
let(:user) do
described_class.new(options)
end
it 'returns the option' do
expect(user.auth_mech_properties).to eq(auth_mech_properties)
end
end
context 'when no option is provided' do
let(:user) do
described_class.new(options)
end
it 'returns an empty hash' do
expect(user.auth_mech_properties).to eq({})
end
end
end
describe '#roles' do
context 'when roles are provided' do
let(:roles) do
[ Mongo::Auth::Roles::ROOT ]
end
let(:user) do
described_class.new(roles: roles)
end
it 'returns the roles' do
expect(user.roles).to eq(roles)
end
end
context 'when no roles are provided' do
let(:user) do
described_class.new({})
end
it 'returns an empty array' do
expect(user.roles).to be_empty
end
end
end
describe '#spec' do
context 'when no password and no roles are set' do
let(:user) do
described_class.new(user: 'foo')
end
it 'is a hash with empty roles' do
user.spec.should == {roles: []}
end
end
end
end
|