-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f3a0340
commit 30c0f60
Showing
6 changed files
with
136 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
require 'test_helper' | ||
|
||
class Bsa::AuthServiceTest < ActiveSupport::TestCase | ||
|
||
def test_for_succesfull_authentication | ||
service_instance = Bsa::AuthService.new(redis_required: false) | ||
service_instance.redis.del('bsa_token') | ||
|
||
token = generate_test_bsa_token(Time.zone.now + 10.minute) | ||
stub_succesfull_request(token) | ||
|
||
r = service_instance.call | ||
|
||
assert r.result? | ||
assert_equal r.body.id_token, token | ||
|
||
stored_token = service_instance.redis.get('bsa_token') | ||
assert_nil stored_token | ||
end | ||
|
||
def test_for_failed_authentication | ||
stub_failed_request | ||
|
||
r = Bsa::AuthService.call(redis_required: false) | ||
|
||
refute r.result? | ||
assert_equal r.error.message, 'NOT_FOUND' | ||
assert_equal r.error.description, 'User not found' | ||
end | ||
|
||
def test_token_should_be_put_in_redis_if_redis_available | ||
service_instance = Bsa::AuthService.new(redis_required: true) | ||
service_instance.redis.del('bsa_token') | ||
|
||
token = generate_test_bsa_token(Time.zone.now + 10.minute) | ||
stub_succesfull_request(token) | ||
|
||
r = service_instance.call | ||
|
||
assert r.result? | ||
assert_equal r.body.id_token, token | ||
|
||
|
||
stored_token = service_instance.redis.get('bsa_token') | ||
|
||
assert_equal token, stored_token | ||
end | ||
|
||
def test_token_shoule_be_get_from_redis_if_redis_available | ||
# To verify that the data is coming from redis, I will not mock a web query | ||
token = generate_test_bsa_token(Time.zone.now + 10.minute) | ||
|
||
service_instance = Bsa::AuthService.new(redis_required: true) | ||
service_instance.redis.set('bsa_token', token) | ||
|
||
r = service_instance.call | ||
|
||
assert r.result? | ||
assert_equal r.body.id_token, token | ||
end | ||
|
||
def test_token_should_be_required_if_token_is_expired | ||
token = generate_test_bsa_token(Time.zone.now - 20.minute) | ||
|
||
service_instance = Bsa::AuthService.new(redis_required: true) | ||
service_instance.redis.set('bsa_token', token) | ||
|
||
token = generate_test_bsa_token(Time.zone.now + 20.minute) | ||
stub_succesfull_request(token) | ||
|
||
r = service_instance.call | ||
|
||
assert r.result? | ||
assert_equal r.body.id_token, token | ||
end | ||
|
||
private | ||
|
||
def stub_succesfull_request(token) | ||
stub_request(:post, "https://api-ote.bsagateway.co/iam/api/authenticate/apiKey") | ||
.to_return( | ||
status: 200, | ||
body: { id_token: token }.to_json, | ||
headers: { 'Content-Type' => 'application/json' } | ||
) | ||
end | ||
|
||
def stub_failed_request | ||
stub_request(:post, "https://api-ote.bsagateway.co/iam/api/authenticate/apiKey") | ||
.to_return( | ||
status: 401, | ||
body: { message: 'NOT_FOUND', description: "User not found" }.to_json, | ||
headers: { 'Content-Type' => 'application/json' } | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters