-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First cut of code for pushing permissions to apps
- Loading branch information
1 parent
9fbc8cc
commit f339848
Showing
8 changed files
with
154 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
GDS_API_CREDENTIALS = { | ||
basic_auth: { | ||
user: 'api', | ||
password: 'defined_on_rollout_not' | ||
}} |
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,16 @@ | ||
require 'gds_api/base' | ||
|
||
class GdsApi::SSO < GdsApi::Base | ||
def initialize(options) | ||
super(nil, options) | ||
end | ||
|
||
def update_user(user) | ||
put_json!("#{base_url}/user", JSON.parse(user)) | ||
end | ||
|
||
private | ||
def base_url | ||
"#{@endpoint}/auth/gds/api" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
class PropagatePermissions | ||
def initialize(permissions) | ||
@user = permissions.first.user | ||
@applications = permissions.map(&:application) | ||
end | ||
|
||
def attempt | ||
results = { successes: [], failures: [] } | ||
@applications.each do |application| | ||
begin | ||
update_application(@user, application) | ||
results[:successes] << { application: application } | ||
rescue URI::InvalidURIError | ||
results[:failures] << { application: application, message: "Haven't got a valid URL for that app.", technical: "URL I have is: #{application.redirect_uri}" } | ||
rescue GdsApi::EndpointNotFound, SocketError => e | ||
results[:failures] << { application: application, message: "Couldn't find the app. Maybe the app is down?", technical: e.message } | ||
rescue GdsApi::TimedOutException | ||
results[:failures] << { application: application, message: "Timed out. Maybe the app is down?" } | ||
rescue GdsApi::HTTPErrorResponse => e | ||
message = case e.code | ||
when 404 | ||
"This app doesn't seem to support syncing of permissions." | ||
when 502 | ||
"Couldn't find the app. Maybe the app is down?" | ||
else | ||
e.message | ||
end | ||
results[:failures] << { application: application, message: message, technical: "HTTP status code was: #{e.code}" } | ||
rescue GdsApi::BaseError, StandardError => e | ||
results[:failures] << { application: application, message: e.message } | ||
end | ||
end | ||
results | ||
end | ||
|
||
private | ||
def update_application(user, application) | ||
options = { endpoint_url: application.url_without_path }.merge(GDS_API_CREDENTIALS) | ||
api = GdsApi::SSO.new(options) | ||
api.update_user(user.to_sensible_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
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,60 @@ | ||
require 'test_helper' | ||
|
||
class PropagatePermissionsTest < ActiveSupport::TestCase | ||
|
||
def url_for_app(application) | ||
url = URI.parse(application.redirect_uri) | ||
"http://api:defined_on_rollout_not@#{url.host}/auth/gds/api/user" | ||
end | ||
|
||
setup do | ||
@user = FactoryGirl.create(:user) | ||
@application = FactoryGirl.create(:application, redirect_uri: "http://app.com/callback") | ||
@permission = FactoryGirl.create(:permission, | ||
application: @application, | ||
user: @user, | ||
permissions: ["ba"]) | ||
end | ||
|
||
should "send a PUT to the related app with the user.json as in the OAuth exchange" do | ||
expected_body = @user.to_sensible_json | ||
expected_url = | ||
request = stub_request(:put, url_for_app(@application)).with(body: expected_body) | ||
PropagatePermissions.new([@permission]).attempt | ||
assert_requested request | ||
end | ||
|
||
should "return a structure of successful and failed pushes" do | ||
not_supported_yet_app = FactoryGirl.create(:application, redirect_uri: "http://not-supported-yet.com/callback") | ||
FactoryGirl.create(:permission, | ||
application: not_supported_yet_app, | ||
user: @user, | ||
permissions: ["ba"]) | ||
|
||
slow_app = FactoryGirl.create(:application, redirect_uri: "http://slow.com/callback") | ||
FactoryGirl.create(:permission, | ||
application: slow_app, | ||
user: @user, | ||
permissions: ["ba"]) | ||
|
||
stub_request(:put, url_for_app(@application)).to_return(status: 200) | ||
stub_request(:put, url_for_app(not_supported_yet_app)).to_return(status: 404) | ||
stub_request(:put, url_for_app(slow_app)).to_timeout | ||
|
||
results = PropagatePermissions.new(@user.permissions).attempt | ||
|
||
assert_equal [{ application: @application }], results[:successes] | ||
expected_failures = [ | ||
{ | ||
application: not_supported_yet_app, | ||
message: "This app doesn't seem to support syncing of permissions.", | ||
technical: "HTTP status code was: 404" | ||
}, | ||
{ | ||
application: slow_app, | ||
message: "Timed out. Maybe the app is down?" | ||
} | ||
] | ||
assert_equal expected_failures, results[:failures] | ||
end | ||
end |