require 'spec_helper' describe SoarAuthenticationToken::TokenGenerator do before :all do keypair_generator = SoarAuthenticationToken::KeypairGenerator.new @private_key, @public_key = keypair_generator.generate @test_authenticated_identifier = 'a@b.co.za' end before :each do @generator_configuration_local = { 'mode' => 'local', 'private_key' => @private_key } @validator_configuration_local = { 'mode' => 'local', 'public_key' => @public_key } @configuration_remote = { 'mode' => 'remote', 'generator-url' => 'http://authentication-token-generator-service:9393/generate', 'validator-url' => 'http://authentication-token-validator-service:9393/validate', 'generator-client-auth-token' => 'test_ecosystem_token_for_auth_token_aaapi_authenticator_service' } @test_store = AuthTokenStoreProvider::StubClient.new end after :each do end after :all do end it 'has a version number' do expect(SoarAuthenticationToken::VERSION).not_to be nil end context "when generating a new token locally" do it 'should generate the token locally using configured private key' do @iut = SoarAuthenticationToken::TokenGenerator.new(@generator_configuration_local) @iut.inject_store_provider(@test_store) @validator = SoarAuthenticationToken::TokenValidator.new(@validator_configuration_local) @validator.inject_store_provider(@test_store) token, token_generator_meta = @iut.generate(authenticated_identifier: @test_authenticated_identifier, flow_identifier: 'test-flow-id') token_validity, token_validator_meta = @validator.validate(authentication_token: token, flow_identifier: 'test-flow-id') expect(token_validity).to eq(true) expect(token_validator_meta['authenticated_identifier']).to eq(@test_authenticated_identifier) end end context "when generating a new token remotely" do it 'should request the token from the configured remote service' do @iut = SoarAuthenticationToken::TokenGenerator.new(@configuration_remote) @iut.inject_store_provider(@test_store) token, token_generator_meta = @iut.generate(authenticated_identifier: @test_authenticated_identifier, flow_identifier: 'test-flow-id') @validator = SoarAuthenticationToken::TokenValidator.new(@configuration_remote) @iut.inject_store_provider(@test_store) token_validity, token_validator_meta, messages = @validator.validate(authentication_token: token, flow_identifier: 'test-flow-id') expect(token_validity).to eq(true) expect(token_validator_meta['authenticated_identifier']).to eq(@test_authenticated_identifier) expect(messages).to match(/Valid token/) end end end