-
Notifications
You must be signed in to change notification settings - Fork 0
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 #192 from alces-flight/teams/default-team
Create user team on sign up
- Loading branch information
Showing
16 changed files
with
347 additions
and
118 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class CreateSingleUserTeamJob < ApplicationJob | ||
queue_as :default | ||
|
||
def perform(user, cloud_service_config) | ||
team = nil | ||
team_role = nil | ||
|
||
ActiveRecord::Base.transaction do | ||
team = Team.new(name: "#{user.login}_team", single_user: true) | ||
unless team.save | ||
logger.info("Unable to create team for #{user.login} #{team.errors.details}") | ||
raise ActiveModel::ValidationError, team | ||
end | ||
|
||
team_role = TeamRole.new(team: team, user: user, role: "admin") | ||
unless team_role.save | ||
logger.info("Unable to create team role for #{user.login} #{team_role.errors.details}") | ||
logger.info("Rolling back creation of team #{team.name}") | ||
raise ActiveModel::ValidationError, team_role | ||
end | ||
end | ||
|
||
CreateTeamThenRoleJob.perform_later(team, team_role, cloud_service_config) | ||
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,26 @@ | ||
require 'faraday' | ||
|
||
class CreateTeamThenRoleJob < CreateTeamJob | ||
def perform(team, team_role, cloud_service_config, **options) | ||
runner = Runner.new( | ||
team: team, | ||
team_role: team_role, | ||
cloud_service_config: cloud_service_config, | ||
logger: logger, | ||
**options | ||
) | ||
runner.call | ||
end | ||
|
||
class Runner < CreateTeamJob::Runner | ||
def initialize(team_role:, **kwargs) | ||
@team_role = team_role | ||
super(**kwargs) | ||
end | ||
|
||
def call | ||
super | ||
CreateTeamRoleJob.perform_later(@team_role, @cloud_service_config) | ||
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
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 @@ | ||
class AddSingleUserToTeam < ActiveRecord::Migration[7.1] | ||
def change | ||
add_column :teams, :single_user, :boolean, default: false, null: false | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 'rails_helper' | ||
|
||
RSpec.describe CreateSingleUserTeamJob, type: :job do | ||
include ActiveJob::TestHelper | ||
let(:stubs) { Faraday::Adapter::Test::Stubs.new } | ||
let!(:user) { create(:user, :with_openstack_account, login: "bilbo") } | ||
let(:changes) { {} } | ||
let(:cloud_service_config) { create(:cloud_service_config) } | ||
|
||
subject(:job) { | ||
CreateSingleUserTeamJob.perform_now(user, cloud_service_config) | ||
} | ||
|
||
before(:each) do | ||
clear_enqueued_jobs | ||
clear_performed_jobs | ||
end | ||
|
||
it "creates a single user team with the user's username" do | ||
expect { subject }.to change(Team, :count).by(1) | ||
team = Team.last | ||
expect(team.name).to eq "bilbo_team" | ||
expect(team.single_user).to eq true | ||
end | ||
|
||
it "assigns user as team admin" do | ||
expect { subject }.to change(TeamRole, :count).by(1) | ||
role = TeamRole.last | ||
team = Team.last | ||
expect(role.user).to eq user | ||
expect(role.team).to eq team | ||
expect(role.role).to eq 'admin' | ||
end | ||
|
||
it "rolls back creation of team if user assignment fails" do | ||
user.root = true | ||
expect { subject rescue nil }.to not_change(Team, :count).and not_change(TeamRole, :count) | ||
|
||
expect(CreateTeamThenRoleJob).not_to have_been_enqueued | ||
end | ||
|
||
it "enqueues creation of a team in openstack" do | ||
subject | ||
expect(CreateTeamThenRoleJob).to have_been_enqueued.with(Team.last, TeamRole.last, cloud_service_config) | ||
end | ||
|
||
it "does not enqueue creation of team in openstack if unsuccessful" do | ||
create(:team, name: "bilbo_team") | ||
expect { subject rescue nil }.not_to change(Team, :count) | ||
expect(CreateTeamThenRoleJob).not_to have_been_enqueued | ||
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,55 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe CreateTeamThenRoleJob, type: :job do | ||
include ActiveJob::TestHelper | ||
let(:stubs) { Faraday::Adapter::Test::Stubs.new } | ||
let(:cloud_service_config) { create(:cloud_service_config) } | ||
let(:team) { create(:team) } | ||
let(:team_role) { create(:team_role, team: team) } | ||
|
||
subject(:job_runner) { | ||
CreateTeamThenRoleJob::Runner.new(team: team, team_role: team_role, cloud_service_config: cloud_service_config, test_stubs: stubs) | ||
} | ||
|
||
include_examples 'creating team job' | ||
|
||
describe "creating role on success" do | ||
let(:team_service_path) { "/create_team" } | ||
|
||
before(:each) do | ||
clear_enqueued_jobs | ||
clear_performed_jobs | ||
end | ||
|
||
context "when team creation request succeeds" do | ||
let(:project_id) { SecureRandom.uuid } | ||
let(:billing_acct_id) { SecureRandom.uuid } | ||
let(:response_body) { | ||
{project_id: project_id, billing_account_id: billing_acct_id} | ||
.stringify_keys | ||
} | ||
|
||
before(:each) do | ||
stubs.post(team_service_path) { |env| [ 201, {}, response_body ] } | ||
end | ||
|
||
it "enqueues job to create role" do | ||
subject.call | ||
expect(CreateTeamRoleJob).to have_been_enqueued.with(team_role, cloud_service_config) | ||
end | ||
end | ||
|
||
context "when team creation request fails" do | ||
let(:response_body) { {} } | ||
|
||
before(:each) do | ||
stubs.post(team_service_path) { |env| [ 201, {}, response_body ] } | ||
end | ||
|
||
it "does not enqueue role creation" do | ||
subject.call rescue nil | ||
expect(CreateTeamRoleJob).not_to have_been_enqueued | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.