Skip to content

Commit

Permalink
Refactored spec for auth based on new setup
Browse files Browse the repository at this point in the history
  • Loading branch information
louispt1 committed Nov 26, 2024
1 parent be19c69 commit bc4c63f
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 42 deletions.
Binary file added app/assets/images/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!DOCTYPE html>
<html>
<head>
<title>Invitation Email</title>
<style>
p {font-family: Arial, sans-serif; color: #333;}
p a {color: #007BFF; text-decoration: none;}
p.light {
color: #777;
font-size: 14px;
margin-bottom: 30px;
}

.box-around-image {
background-color: #4e98e4;
padding: 10px 10px 5px;
border-radius: 3px;
display: block;
width: 200px
}
</style>
</head>
<body>
<p>Hello!</p>

<p>
<%= @inviter_name %> has just invited you to collaborate on the scenario "<%= @saved_scenario_title %>" of the Energy Transition Model as <%=t("scenario_invitation_mailer.roles.#{@new_role}")%>.
</p>
<p>
If you already have an account with <a href="<%= Settings.etmodel_uri %>">https://energytransitionmodel.com/</a>, follow <a href="<%= @saved_scenario_link %>">this link to view the scenario</a> and start collaborating!
</p>

<p>
If you don't have an account yet, click <a href="<%= new_user_registration_url %>">here to register</a>.
</p>

<p class="light">
If you weren't expecting this invitation, please ignore this email.
</p>
<a class="box-around-image" href="<%= Settings.etmodel_uri %>">
<%=email_inline_image_tag("logo.png", style: "width: 200px; height: auto")%>
</a>

</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!DOCTYPE html>
<html>
<head>
<title>Uitnodigingsemail</title>
<style>
p {font-family: Arial, sans-serif; color: #333;}
p a {color: #007BFF; text-decoration: none;}
p.light {
color: #777;
font-size: 14px;
margin-bottom: 30px;
}

.box-around-image {
background-color: #4e98e4;
padding: 10px 10px 5px;
border-radius: 3px;
display: block;
width: 200px
}
</style>
</head>
<body>
<p>Hallo!</p>

<p>
<%= @inviter_name %> heeft je zojuist uitgenodigd om samen te werken aan het scenario "<%= @saved_scenario_title %>" van het Energietransitiemodel als <%=t("scenario_invitation_mailer.roles.#{@new_role}")%>.
</p>
<p>
Als je al een account hebt bij <a href="<%= Settings.etmodel_uri %>">https://energytransitionmodel.com/</a>, volg dan <a href="<%= @saved_scenario_link %>">deze link om het scenario te bekijken</a> en begin met samenwerken!
</p>

<p>
Als je nog geen account hebt, klik dan <a href="<%= new_user_registration_url %>">hier om je aan te melden</a>.
</p>

<p class="light">
Als je deze uitnodiging niet verwachtte, negeer dan deze e-mail.
</p>
<a class="box-around-image" href="<%= Settings.etmodel_uri %>">
<%=email_inline_image_tag("logo.png", style: "width: 200px; height: auto")%>
</a>

</body>
</html>
2 changes: 1 addition & 1 deletion lib/myetm/auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def user_jwt(user = nil, scopes: [], client_id: nil)
payload = {
iss: Doorkeeper::OpenidConnect.configuration.issuer.call(user, nil),
aud: client_id,
exp: 1.hour.from_now.to_i,
exp: 1.minute.from_now.to_i,
iat: Time.now.to_i,
scopes: scopes,
sub: user.id,
Expand Down
2 changes: 1 addition & 1 deletion spec/jobs/identity/destroy_user_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
Settings.etmodel_uri = 'http://example.org'

allow(MyEtm::Auth)
.to receive(:client_app_client)
.to receive(:model_client)
.with(user)
.and_return(connection)

Expand Down
2 changes: 1 addition & 1 deletion spec/jobs/identity/sync_user_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
Settings.etmodel_uri = 'http://example.org'

allow(MyEtm::Auth)
.to receive(:client_app_client)
.to receive(:model_client)
.with(user)
.and_return(connection)

Expand Down
67 changes: 28 additions & 39 deletions spec/myetm/auth_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,55 @@

RSpec.describe MyEtm::Auth do
describe '.user_jwt' do
subject do
subject(:decoded_jwt) do
JWT.decode(
described_class.user_jwt(user, scopes: %w[read write]),
token,
described_class.signing_key.public_key,
true,
algorithm: 'RS256'
)
end

before { Settings.etmodel_uri = 'http://etmodel.test' }
let(:token) { described_class.user_jwt(user, scopes: scopes, client_id: client_id) }
let(:user) { create(:user) }
let(:scopes) { %w[read write] }
let(:client_id) { 'test-client-id' }

after { Settings.reload! }
let(:payload) { decoded_jwt[0] }
let(:header) { decoded_jwt[1] }

let(:user) { create(:user) }
before do
Settings.etmodel_uri = 'http://etmodel.test'
end

let(:payload) { subject[0] }
let(:header) { subject[1] }
after do
Settings.reload!
end

it 'returns a JWT for the given user' do
expect(payload['user']).to eq(user.as_json(only: %i[id name]))
expect(payload['user']).to eq(user.as_json(only: %i[admin id]))
end

it 'includes the scopes in the JWT payload' do
expect(payload['scopes']).to eq(%w[read write])
expect(payload['scopes']).to eq(scopes)
end

it 'includes the issuer in the JWT payload' do
expect(payload['iss']).to eq(Doorkeeper::OpenidConnect.configuration.issuer.call(user, nil))
expect(payload['iss']).to eq(Doorkeeper::OpenidConnect.configuration.issuer.call(user, nil))
end

pending 'includes the audience in the JWT payload' do
expect(payload['aud']).to eq(Settings.etmodel_uri)
it 'includes the audience in the JWT payload' do
expect(payload['aud']).to eq(client_id)
end

it 'includes the expiration time in the JWT payload' do
expect(payload['exp']).to be_within(1).of(1.minute.from_now.to_i)
expected_exp = (Time.now + 1.minute).to_i
expect(payload['exp']).to be_within(1).of(expected_exp)
end

it 'includes the issued at time in the JWT payload' do
expect(payload['iat']).to be_within(1).of(Time.now.to_i)
expected_iat = Time.now.to_i
expect(payload['iat']).to be_within(1).of(expected_iat)
end

it 'includes the subject in the JWT payload' do
Expand All @@ -52,32 +61,12 @@
expect(header['kid']).to eq(described_class.signing_key.to_jwk['kid'])
end

pending 'raises an error when no ETModel URI is set' do
Settings.etmodel_uri = nil

expect { described_class.user_jwt(build(:user)) }.to raise_error(
"No ETModel URI. Please set the 'etmodel_uri' setting in config/settings.local.yml."
)
end
end

describe '.client_app_client' do
subject do
described_class.client_app_client(user, etmodel)
end

before { Settings.etmodel_uri = 'http://etmodel.test' }

after { Settings.reload! }

let(:user) { create(:user) }

pending 'sets the scheme for the client' do
expect(subject.scheme).to eq('http')
end
context 'when client_id is not provided' do
let(:client_id) { nil }

pending 'sets the host for the client' do
expect(subject.host).to eq('etmodel.test')
it 'does not include an audience in the JWT payload' do
expect(payload['aud']).to eq(nil)
end
end
end
end

0 comments on commit bc4c63f

Please sign in to comment.