This repository has been archived by the owner on Sep 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1059 from fastlane/create-account
Add new API endpoint to register user
- Loading branch information
Showing
5 changed files
with
102 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
require_relative "../services/user_service" | ||
require_relative "api_controller" | ||
require_relative "json_params" | ||
|
||
module FastlaneCI | ||
# Controller responsible for handling users | ||
class UserJSONController < APIController | ||
disable(:authentication) | ||
|
||
HOME = "/api/user" | ||
|
||
post HOME.to_s do | ||
# fetch email based on the API token instead | ||
github_client = Octokit::Client.new(access_token: params[:github_token]) | ||
|
||
begin | ||
# Note: This fails if the user.email scope is missing from token | ||
email = github_client.emails.find(&:primary).email | ||
rescue Octokit::NotFound | ||
json_error!( | ||
error_message: "Provided API token needs user email scope", | ||
error_key: "User.Token.MissingEmailScope", | ||
error_code: 400 | ||
) | ||
rescue Octokit::Unauthorized | ||
json_error!( | ||
error_message: "Provided API token is invalid", | ||
error_key: "User.Token.Invalid", | ||
error_code: 403 | ||
) | ||
end | ||
|
||
user = Services.user_service.create_user!( | ||
email: email, | ||
password: params[:password] | ||
) | ||
|
||
if user | ||
Services.user_service.create_provider_credential!( | ||
user_id: user.id, | ||
email: user.email, | ||
api_token: params[:github_token] | ||
) | ||
return json({ status: :success }) | ||
else | ||
json_error!( | ||
error_message: "Error creating new user", | ||
error_key: "User.Error" | ||
) | ||
end | ||
end | ||
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
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,45 @@ | ||
require "spec_helper" | ||
require "app/features-json/user_json_controller" | ||
require "app/services/user_service" | ||
|
||
describe FastlaneCI::UserJSONController do | ||
def app | ||
described_class | ||
end | ||
let(:json) { JSON.parse(last_response.body) } | ||
|
||
before do | ||
allow(FastlaneCI.dot_keys).to receive(:encryption_key).and_return("test") | ||
end | ||
|
||
describe "/api/user" do | ||
let(:fake_email) { "email@email.com" } | ||
before do | ||
github_client = "github_client" | ||
expect(Octokit::Client).to receive(:new).and_return(github_client) | ||
|
||
email_entry = "email_entry" | ||
expect(email_entry).to receive(:primary).and_return(true) | ||
expect(email_entry).to receive(:email).and_return(fake_email) | ||
|
||
expect(github_client).to receive(:emails).and_return([email_entry]) | ||
end | ||
|
||
it "creates a new user and attach the provider credentials" do | ||
allow(FastlaneCI::Services.user_service.user_data_source).to receive(:user_exist?).with({ email: fake_email }).and_return(false) | ||
|
||
post "/api/user", { github_token: "github_token", password: "password" }.to_json, { "CONTENT_TYPE" => "application/json" } | ||
expect(last_response).to be_ok | ||
expect(json["status"]).to eq("success") | ||
end | ||
|
||
it "returns an error if the user already exists" do | ||
allow(FastlaneCI::Services.user_service.user_data_source).to receive(:user_exist?).with({ email: fake_email }).and_return(true) | ||
|
||
post "/api/user", { github_token: "github_token", password: "password" }.to_json, { "CONTENT_TYPE" => "application/json" } | ||
expect(last_response.status).to eq(400) | ||
expect(json["key"]).to eq("User.Error") | ||
expect(json["message"]).to eq("Error creating new user") | ||
end | ||
end | ||
end |