Commit 718ea942 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Improve test cases description in token-related specs

parent 636b038e
...@@ -15,8 +15,6 @@ module TokenAuthenticatableStrategies ...@@ -15,8 +15,6 @@ module TokenAuthenticatableStrategies
end end
token_authenticatable token_authenticatable
rescue ActiveRecord::StatementInvalid
nil
end end
def ensure_token(instance) def ensure_token(instance)
...@@ -38,8 +36,8 @@ module TokenAuthenticatableStrategies ...@@ -38,8 +36,8 @@ module TokenAuthenticatableStrategies
end end
def get_token(instance) def get_token(instance)
raw_token = instance.read_attribute(encrypted_field) encrypted_token = instance.read_attribute(encrypted_field)
token = Gitlab::CryptoHelper.aes256_gcm_decrypt(raw_token) token = Gitlab::CryptoHelper.aes256_gcm_decrypt(encrypted_token)
token || (fallback_strategy.get_token(instance) if fallback?) token || (fallback_strategy.get_token(instance) if fallback?)
end end
...@@ -61,7 +59,7 @@ module TokenAuthenticatableStrategies ...@@ -61,7 +59,7 @@ module TokenAuthenticatableStrategies
def token_set?(instance) def token_set?(instance)
raw_token = instance.read_attribute(encrypted_field) raw_token = instance.read_attribute(encrypted_field)
raw_token ||= (instance.read_attribute(token_field) if fallback?) raw_token ||= (fallback_strategy.get_token(instance) if fallback?)
raw_token.present? raw_token.present?
end end
......
...@@ -13,7 +13,7 @@ module Gitlab ...@@ -13,7 +13,7 @@ module Gitlab
self.table_name = 'ci_runners' self.table_name = 'ci_runners'
self.inheritance_column = :_type_disabled self.inheritance_column = :_type_disabled
def runners_token=(value) def token=(value)
self.token_encrypted = self.token_encrypted =
::Gitlab::CryptoHelper.aes256_gcm_encrypt(value) ::Gitlab::CryptoHelper.aes256_gcm_encrypt(value)
end end
......
...@@ -771,13 +771,13 @@ describe Ci::Build do ...@@ -771,13 +771,13 @@ describe Ci::Build do
context 'hide runners token' do context 'hide runners token' do
let(:data) { "new #{project.runners_token} data"} let(:data) { "new #{project.runners_token} data"}
it { is_expected.to match(/^new [x]+ data$/) } it { is_expected.to match(/^new x+ data$/) }
end end
context 'hide build token' do context 'hide build token' do
let(:data) { "new #{build.token} data"} let(:data) { "new #{build.token} data"}
it { is_expected.to match(/^new [x]+ data$/) } it { is_expected.to match(/^new x+ data$/) }
end end
end end
......
...@@ -14,7 +14,7 @@ describe TokenAuthenticatableStrategies::Encrypted do ...@@ -14,7 +14,7 @@ describe TokenAuthenticatableStrategies::Encrypted do
end end
describe '#find_token_authenticatable' do describe '#find_token_authenticatable' do
it 'finds a relevant resource by encrypted value' do it 'finds the encrypted resource by cleartext' do
allow(model).to receive(:find_by) allow(model).to receive(:find_by)
.with('some_field_encrypted' => encrypted) .with('some_field_encrypted' => encrypted)
.and_return('encrypted resource') .and_return('encrypted resource')
...@@ -23,8 +23,8 @@ describe TokenAuthenticatableStrategies::Encrypted do ...@@ -23,8 +23,8 @@ describe TokenAuthenticatableStrategies::Encrypted do
.to eq 'encrypted resource' .to eq 'encrypted resource'
end end
it 'uses fallback strategy when token can not be found' do it 'uses fallback strategy when encrypted token cannot be found' do
allow_any_instance_of(TokenAuthenticatableStrategies::Insecure) allow(subject.send(:fallback_strategy))
.to receive(:find_token_authenticatable) .to receive(:find_token_authenticatable)
.and_return('plaintext resource') .and_return('plaintext resource')
...@@ -38,7 +38,7 @@ describe TokenAuthenticatableStrategies::Encrypted do ...@@ -38,7 +38,7 @@ describe TokenAuthenticatableStrategies::Encrypted do
end end
describe '#get_token' do describe '#get_token' do
it 'decrypts a token when encrypted token is present' do it 'returns decrypted token when an encrypted token is present' do
allow(instance).to receive(:read_attribute) allow(instance).to receive(:read_attribute)
.with('some_field_encrypted') .with('some_field_encrypted')
.and_return(encrypted) .and_return(encrypted)
...@@ -46,7 +46,7 @@ describe TokenAuthenticatableStrategies::Encrypted do ...@@ -46,7 +46,7 @@ describe TokenAuthenticatableStrategies::Encrypted do
expect(subject.get_token(instance)).to eq 'my-value' expect(subject.get_token(instance)).to eq 'my-value'
end end
it 'reads a plaintext token when encrypted token is not present' do it 'returns the plaintext token when encrypted token is not present' do
allow(instance).to receive(:read_attribute) allow(instance).to receive(:read_attribute)
.with('some_field_encrypted') .with('some_field_encrypted')
.and_return(nil) .and_return(nil)
...@@ -60,7 +60,7 @@ describe TokenAuthenticatableStrategies::Encrypted do ...@@ -60,7 +60,7 @@ describe TokenAuthenticatableStrategies::Encrypted do
end end
describe '#set_token' do describe '#set_token' do
it 'writes encrypted token to a model instance and returns it' do it 'writes encrypted token and removes plaintext token and returns it' do
expect(instance).to receive(:[]=) expect(instance).to receive(:[]=)
.with('some_field_encrypted', encrypted) .with('some_field_encrypted', encrypted)
expect(instance).to receive(:[]=) expect(instance).to receive(:[]=)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment