Skip to content

Commit

Permalink
Merge pull request #200 from alces-flight/teams/new-middleware2
Browse files Browse the repository at this point in the history
Support new middleware
  • Loading branch information
timalces authored Apr 4, 2024
2 parents f601d76 + 0e4451a commit 0025e23
Show file tree
Hide file tree
Showing 30 changed files with 86 additions and 81 deletions.
2 changes: 1 addition & 1 deletion app/controllers/key_pairs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,6 @@ def key_pair_params
# key pairs are user (not project) specific, but membership of a project is required
# to view, create and delete them
def set_project_id
@project_id = current_user.teams.where.not(project_id: nil).first&.project_id
@project_id = current_user.teams.where.not(project_id: nil).where(deleted_at: nil).first&.project_id
end
end
6 changes: 3 additions & 3 deletions app/jobs/create_credit_deposit_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ def url
end

def path
"/add_credits"
"/credits"
end

def body
{
credits: {
billing_account_id: @credit_deposit.billing_acct_id,
credits_to_add: @credit_deposit.amount
billing_acct_id: @credit_deposit.billing_acct_id,
amount: @credit_deposit.amount
}
}
end
Expand Down
6 changes: 3 additions & 3 deletions app/jobs/create_team_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Result
property :project_id, context: :cloud
validates :project_id, presence: true, on: :cloud

property :billing_acct_id, from: :billing_account_id, context: :billing
property :billing_acct_id, context: :billing
validates :billing_acct_id, presence: true, on: :billing
end

Expand All @@ -51,7 +51,7 @@ def call
private

def url
"#{@cloud_service_config.user_handler_base_url}/create_team"
"#{@cloud_service_config.user_handler_base_url}/team"
end

def body
Expand All @@ -65,7 +65,7 @@ def body
name: @team.name
}.tap do |h|
h[:project_id] = @team.project_id unless @team.project_id.blank?
h[:billing_account_id] = @team.billing_acct_id unless @team.billing_acct_id.blank?
h[:billing_acct_id] = @team.billing_acct_id unless @team.billing_acct_id.blank?
end
end
end
Expand Down
15 changes: 7 additions & 8 deletions app/jobs/create_team_role_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@ class Runner < HttpRequests::Faraday::JobRunner

def initialize(team_role:, **kwargs)
@team_role = team_role
super(**kwargs)
super(**kwargs.reverse_merge(test_stubs: test_stubs))
end

def call
response = connection.post(path, body)
def test_stubs
nil
end

def call
response = super
unless response.success?
return Result.new(false, "#{error_description}: #{response.reason_phrase || "Unknown error"}")
end
Expand All @@ -60,11 +63,7 @@ def call
private

def url
@cloud_service_config.user_handler_base_url
end

def path
"/create_team_role"
"#{@cloud_service_config.user_handler_base_url}/team_role"
end

def body
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/delete_team_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def call

def url
url = URI(@cloud_service_config.user_handler_base_url)
url.path = "/delete_team"
url.path = "/team"
url.to_s
end

Expand Down
2 changes: 1 addition & 1 deletion app/jobs/delete_team_role_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def url
end

def path
"/delete_team_role"
"/team_role"
end

def body
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/get_draft_invoice_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def url
def body
{
invoice: {
billing_account_id: @team.billing_acct_id,
billing_acct_id: @team.billing_acct_id,
target_date: Date.today.to_formatted_s(:iso8601),
},
}
Expand Down
4 changes: 2 additions & 2 deletions app/jobs/get_invoice_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def invoice
private

def parse_body(body)
@invoice = parse_invoice(body["account_invoice"])
@invoice = parse_invoice(body["invoice"])
end
end

Expand Down Expand Up @@ -71,7 +71,7 @@ def url
def body
{
invoice: {
billing_account_id: @team.billing_acct_id,
billing_acct_id: @team.billing_acct_id,
invoice_id: @invoice_id,
},
}
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/get_invoices_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def url
def body
{
invoices: {
billing_account_id: @team.billing_acct_id,
billing_acct_id: @team.billing_acct_id,
offset: @offset,
limit: @limit,
},
Expand Down
3 changes: 1 addition & 2 deletions app/jobs/get_user_key_pairs_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ def call
end

results = response.body
key_pairs = results["key_pairs"].map do |key_pair|
details = key_pair["keypair"]
key_pairs = results["key_pairs"].map do |details|
KeyPair.new(user: @user, name: details["name"], fingerprint: details["fingerprint"], key_type: details["type"] || "ssh")
end
return Result.new(true, key_pairs, "")
Expand Down
4 changes: 2 additions & 2 deletions app/jobs/update_team_role_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_stubs
end

def call
response = super
response = connection.patch("", body)

unless response.success?
return Result.new(false, "#{error_description}: #{response.reason_phrase || "Unknown error"}")
Expand All @@ -67,7 +67,7 @@ def call
private

def url
"#{@cloud_service_config.user_handler_base_url}/update_team_role"
"#{@cloud_service_config.user_handler_base_url}/team_role"
end

def body
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/user_deletion_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def call

def url
url = URI(@cloud_service_config.user_handler_base_url)
url.path = "/delete_user"
url.path = "/user"
url.to_s
end

Expand Down
5 changes: 3 additions & 2 deletions app/jobs/user_signup_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def perform(user, cloud_service_config, **options)
class Result
include HttpRequests::ResultSyncer

property :cloud_user_id, from: :user_id, context: :cloud
property :cloud_user_id, from: :user_cloud_id, context: :cloud
validates :cloud_user_id, presence: true, on: :cloud
end

Expand All @@ -47,7 +47,7 @@ def call
private

def url
"#{@cloud_service_config.user_handler_base_url}/create_user"
"#{@cloud_service_config.user_handler_base_url}/user"
end

def body
Expand All @@ -59,6 +59,7 @@ def body
project_id: @cloud_service_config.admin_project_id,
},
username: @user.login,
name: @user.name,
password: @user.foreign_password,
email: @user.email
}
Expand Down
4 changes: 2 additions & 2 deletions app/jobs/user_update_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_stubs
end

def call
response = super
response = connection.patch("", body)
if response.success?
@user.foreign_password = @user.pending_foreign_password
@user.pending_foreign_password = nil
Expand All @@ -44,7 +44,7 @@ def call

def url
url = URI(@cloud_service_config.user_handler_base_url)
url.path = "/change_user_details"
url.path = "/user"
url.to_s
end

Expand Down
4 changes: 2 additions & 2 deletions app/models/device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class Device < ApplicationRecord
presence: true,
length: { maximum: 150 },
format: {
with: /\A[a-zA-Z0-9\-]*\Z/,
message: "can contain only alphanumeric characters and hyphens."
with: /\A[a-zA-Z0-9\-\.]*\Z/,
message: "can contain only alphanumeric characters, dots and hyphens."
}
validates :status,
presence: true,
Expand Down
6 changes: 3 additions & 3 deletions app/views/api/v1/devices/show.rabl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ end

glue :details do |details|
extends "api/v1/devices/details/#{details.class.name.split('::').last.underscore}"
#node :type do |details|
# details.class.name
#end
node :type do |details|
details.class.name
end
end

attribute :template_id, unless: @include_full_template_details
4 changes: 2 additions & 2 deletions app/views/key_pairs/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

<div class="formItem">
<%= f.label :name, "Key name", class: 'required_field' %>
<%= f.text_field :name, autocomplete: :off, pattern: "^[a-zA-Z][a-zA-Z0-9\\-_]*$" %>
<%= f.text_field :name, autocomplete: :off, pattern: "^[a-zA-Z][a-zA-Z0-9\\-_]*$", required: true %>
</div>

<div class="formItem">
<%= f.label :key_type, "Key type", class: 'required_field' %>
<%= f.select :key_type, [["SSH Key", "ssh"]] %>
<%= f.select :key_type, [["SSH Key", "ssh"]], required: true %>
</div>

<div class="formItem">
Expand Down
9 changes: 9 additions & 0 deletions config/initializers/extended_false_values.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Update list of false values to include Python's False

module ActiveModel::Type
class Boolean
existing_values = FALSE_VALUES.dup
remove_const(:FALSE_VALUES)
FALSE_VALUES = existing_values + ['False', :False]
end
end
2 changes: 1 addition & 1 deletion config/navigation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
primary.item :youraccount, "#{current_user.name}", '#',
align: :right,
icon: :youraccount,
highlights_on: %r(/accounts|/key_pairs/) do |acc|
highlights_on: %r(/accounts|/key_pairs/|/key_pairs) do |acc|
acc.item :acc_details, 'Account details', url_helpers.edit_user_registration_path, :icon => :details, :link => {:class => 'details'}
unless current_user.root?
acc.item :acc_details, 'Manage key-pairs', url_helpers.key_pairs_path,
Expand Down
8 changes: 4 additions & 4 deletions spec/jobs/create_credit_deposit_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
let(:stubs) { Faraday::Adapter::Test::Stubs.new }
let(:cloud_service_config) { create(:cloud_service_config) }
let(:team) { create(:team, :with_openstack_details) }
let(:path) { "#{cloud_service_config.user_handler_base_url}/add_credits" }
let(:path) { "#{cloud_service_config.user_handler_base_url}/credits" }
let(:credit_deposit) { build(:credit_deposit, team: team) }
subject { CreateCreditDepositJob::Runner.new(credit_deposit: credit_deposit, cloud_service_config: cloud_service_config) }

Expand All @@ -21,7 +21,7 @@ class << subject
end

it "uses a hard-coded path" do
expect(subject.path).to eq "/add_credits"
expect(subject.path).to eq "/credits"
end
end

Expand Down Expand Up @@ -76,8 +76,8 @@ class << subject

it 'contains credit deposit details' do
expect(subject[:credits]).to eq({
"billing_account_id" => credit_deposit.billing_acct_id,
"credits_to_add" => credit_deposit.amount
"billing_acct_id" => credit_deposit.billing_acct_id,
"amount" => credit_deposit.amount
})
end
end
Expand Down
29 changes: 15 additions & 14 deletions spec/jobs/create_team_role_job_spec.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
require 'rails_helper'

RSpec.describe UpdateTeamRoleJob, type: :job do
RSpec.describe CreateTeamRoleJob, type: :job do
let(:stubs) { Faraday::Adapter::Test::Stubs.new }
let!(:team_role) { create(:team_role, role: "member") }
let!(:team_role) { build(:team_role, role: "member") }
let(:new_role) { "admin" }
let(:cloud_service_config) { create(:cloud_service_config) }
let(:update_team_roles_path) { "/update_team_role" }
let(:create_team_roles_path) { "/team_role" }
let(:expected_url) {
"#{cloud_service_config.user_handler_base_url}#{update_team_roles_path}"
"#{cloud_service_config.user_handler_base_url}#{create_team_roles_path}"
}

subject { UpdateTeamRoleJob::Runner.new(cloud_service_config: cloud_service_config, team_role: team_role, new_role: new_role) }
subject { CreateTeamRoleJob::Runner.new(cloud_service_config: cloud_service_config, team_role: team_role) }

describe "url" do
subject { super().send(:url) }
Expand All @@ -32,10 +32,11 @@
})
end

it "contains the team role's project and billing ids" do
it "contains the team role's details" do
expect(subject[:team_role]).to be_a Hash
expect(subject[:team_role][:user_id]).to eq team_role.user.cloud_user_id
expect(subject[:team_role][:project_id]).to eq team_role.team.project_id
expect(subject[:team_role][:role]).to eq team_role.role
end
end

Expand All @@ -44,12 +45,12 @@

shared_examples "makes a request to the middleware" do
it "makes a request to the middleware" do
runner = described_class::Runner.new(team_role: team_role, new_role: new_role, cloud_service_config: cloud_service_config)
runner = described_class::Runner.new(team_role: team_role, cloud_service_config: cloud_service_config)
expect(described_class::Runner).to receive(:new)
.with(hash_including(team_role: team_role, new_role: new_role, cloud_service_config: cloud_service_config))
.with(hash_including(team_role: team_role, cloud_service_config: cloud_service_config))
.and_return(runner)
allow(runner).to receive(:call).and_call_original
described_class.perform_now(team_role, new_role, cloud_service_config)
described_class.perform_now(team_role, cloud_service_config)
expect(runner).to have_received(:call)
end
end
Expand All @@ -62,10 +63,10 @@

include_examples "makes a request to the middleware"

it "updates role" do
it "persists role" do
expect {
described_class.perform_now(team_role, new_role, cloud_service_config, test_stubs: stubs)
}.to change(team_role, :role)
described_class.perform_now(team_role, cloud_service_config, test_stubs: stubs)
}.to change(TeamRole, :count).by(1)
end
end

Expand All @@ -79,8 +80,8 @@

it "does not change the role" do
expect {
described_class.perform_now(team_role, new_role ,cloud_service_config)
}.not_to change(team_role, :role)
described_class.perform_now(team_role,cloud_service_config)
}.not_to change(TeamRole, :count)
end
end
end
Expand Down
Loading

0 comments on commit 0025e23

Please sign in to comment.