Skip to content

Commit

Permalink
Update specs and remove debugging code
Browse files Browse the repository at this point in the history
  • Loading branch information
ekumachidi committed Mar 11, 2024
1 parent 32eefd2 commit 30d823d
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 13 deletions.
6 changes: 3 additions & 3 deletions app/controllers/schools/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Schools
class UsersController < BaseController
def index
@users = DFESignInAPI::OrganisationUsers.new(current_user.sub, current_school.urn).users['users']
@users = DFESignInAPI::OrganisationUsers.new(current_user.sub, current_school.urn).users
@dfe_sign_in_request_organisation_url =
Rails.application.config.x.dfe_sign_in_request_organisation_url.presence
end
Expand All @@ -16,8 +16,8 @@ def create

if params[:confirmed] == 'true'
if @user_invite.valid?
invitation_response = @user_invite.invite_user
redirect_to schools_users_path, notice: "#{@user_invite.email} has been added. With Response: #{invitation_response} and #{@user_invite.organisation_id}"
@user_invite.create
redirect_to schools_users_path, notice: "#{@user_invite.email} has been added."
else
render :new
end
Expand Down
2 changes: 1 addition & 1 deletion app/services/schools/dfe_sign_in_api/organisation_users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def ukprn
end

def users
@users ||= response
@users ||= response&.fetch('users', nil)
end

private
Expand Down
9 changes: 1 addition & 8 deletions app/services/schools/dfe_sign_in_api/user_invite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ class UserInvite < Client
validates :email, email_format: true, if: -> { email.present? }
validates :organisation_id, presence: true

def invite_user
@response = response
# @response['success'] = @response['status'] == 'success' if @response.present?
# @response
end

def create
@response = response
end
Expand All @@ -37,8 +31,7 @@ def response
req.body = payload.to_json
end

# JSON.parse(resp.body)
resp
resp.body
end

def service_id
Expand Down
2 changes: 1 addition & 1 deletion app/views/schools/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<%= form_for(@user_invite, url: schools_users_path, method: :post) do |f| %>
<section id="personal-details">
<span class="govuk-caption-xl govuk-!-margin-top-4"> Add user </span>
<h1 class="govuk-heading-l"> Check your answers <%= @user_invite.organisation_id %></h1>
<h1 class="govuk-heading-l"> Check your answers </h1>

<dl class="placement-details govuk-summary-list">
<div class="address govuk-summary-list__row">
Expand Down
76 changes: 76 additions & 0 deletions spec/services/schools/dfe_sign_in_api/organisation_users_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
require 'rails_helper'
require Rails.root.join("spec", "controllers", "schools", "session_context")

describe Schools::DFESignInAPI::OrganisationUsers do
let(:user_uuid) { '123456' }
let(:current_school_urn) { '987654' }
let(:ukprn) { 'organisation-ukprn-id' }
let(:response) do
{
'ukprn' => ukprn,
'users' => [
{
'email' => '[email protected]',
'firstName' => 'user1',
'lastName' => 'test',
'userStatus' => 1,
'roles' => ['role1']
},
{
'email' => '[email protected]',
'firstName' => 'user2',
'lastName' => 'test',
'roles' => ['role1', 'role2']
}
]
}
end

subject { described_class.new(user_uuid, current_school_urn) }

before do
allow(subject).to receive(:response).and_return(response)
allow_any_instance_of(Schools::DFESignInAPI::Organisation).to receive(:current_organisation_ukprn).and_return(ukprn)
end

describe '#initialize' do
it 'sets user_uuid' do
expect(subject.user_uuid).to eq(user_uuid)
end

it 'sets current_school_urn' do
expect(subject.current_school_urn).to eq(current_school_urn)
end
end

describe '#ukprn' do
it 'calls current_organisation_ukprn on Organisation' do
expect_any_instance_of(Schools::DFESignInAPI::Organisation).to receive(:current_organisation_ukprn).and_return(ukprn)
expect(subject.ukprn).to eq(ukprn)
end
end

describe '#users' do
context 'when valid response is received' do
it 'returns the users from the response' do
expect(subject.users).to eq(response['users'])
end
end

context 'when response is nil' do
let(:response) { nil }

it 'returns nil' do
expect(subject.users).to be_nil
end
end

context 'when response does not contain users' do
let(:response) { { 'ukprn' => ukprn } }

it 'returns an empty array' do
expect(subject.users).to eq(nil)
end
end
end
end
42 changes: 42 additions & 0 deletions spec/services/schools/dfe_sign_in_api/user_invite_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'rails_helper'
require Rails.root.join("spec", "controllers", "schools", "session_context")

describe Schools::DFESignInAPI::UserInvite, type: :model do
let(:user_invite) { described_class.new }

describe 'attributes' do
it { is_expected.to respond_to(:email) }
it { is_expected.to respond_to(:firstname) }
it { is_expected.to respond_to(:lastname) }
it { is_expected.to respond_to(:organisation_id) }
end

describe 'validations' do
it { is_expected.to validate_presence_of(:firstname) }
it { is_expected.to validate_presence_of(:lastname) }
it { is_expected.to validate_presence_of(:email) }
it { is_expected.to validate_length_of(:firstname).is_at_most(50) }
it { is_expected.to validate_length_of(:lastname).is_at_most(50) }
it { is_expected.to validate_length_of(:email).is_at_most(100) }
it { is_expected.to allow_value('[email protected]').for(:email) }
it { is_expected.not_to allow_value('invalid_email').for(:email) }
it { is_expected.to validate_presence_of(:organisation_id) }
end

describe '#create' do
context 'when API is enabled' do
it 'makes a POST request to the API endpoint' do
expect(user_invite).to receive(:response)
user_invite.create
end
end

context 'when API is disabled' do
before { allow(user_invite).to receive(:enabled?).and_return(false) }

it 'raises an ApiDisabled error' do
expect { user_invite.create }.to raise_error(Schools::DFESignInAPI::UserInvite::ApiDisabled)
end
end
end
end

0 comments on commit 30d823d

Please sign in to comment.