Skip to content

Commit 642ee0c

Browse files
Add tests
1 parent 47982d9 commit 642ee0c

2 files changed

Lines changed: 82 additions & 6 deletions

File tree

decidim-census/spec/services/census_authorization_handler_when_impersonating_spec.rb

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,43 @@
88
let(:organization) { create(:organization) }
99
let(:user) { create(:user, organization:) }
1010
let(:dni) { "12345678A" }
11-
let!(:scope) { create(:scope, organization:) }
1211
let!(:unique_id) do
1312
Digest::SHA256.hexdigest("#{handler.census_id_document}-#{organization.id}-#{Rails.application.secrets.secret_key_base}")
1413
end
1514
let(:date) { Date.strptime("1990/11/21", "%Y/%m/%d") }
1615

17-
let(:handler) do
18-
CensusAuthorizationHandler.new(document_number: dni, scope_id: scope.id)
19-
end
20-
2116
context "when creating a new impersonation" do
22-
it { is_expected.to eq unique_id }
17+
context "when an scope is provided" do
18+
let!(:scope) { create(:scope, organization:) }
19+
let(:handler) do
20+
CensusAuthorizationHandler.new(document_number: dni, scope_id: scope.id)
21+
end
22+
23+
it { is_expected.to eq unique_id }
24+
end
25+
26+
context "when a user is provided" do
27+
let(:handler) do
28+
CensusAuthorizationHandler.new(document_number: dni, user:)
29+
end
30+
31+
it { is_expected.to eq unique_id }
32+
end
33+
34+
context "when an organization_id is provided" do
35+
let(:handler) do
36+
CensusAuthorizationHandler.new(document_number: dni, organization_id: organization.id)
37+
end
38+
39+
it { is_expected.to eq unique_id }
40+
end
41+
42+
context "when only document_number and birthdate is provided" do
43+
let(:handler) do
44+
CensusAuthorizationHandler.new(document_number: dni, birthdate: date)
45+
end
46+
47+
it { is_expected.not_to eq unique_id }
48+
end
2349
end
2450
end
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# frozen_string_literal: true
2+
3+
require "spec_helper"
4+
5+
RSpec.describe DibaCensusApiAuthorizationHandler do
6+
subject { handler.unique_id }
7+
8+
let(:organization) { create(:organization) }
9+
let(:user) { create(:user, organization:) }
10+
let(:dni) { "12345678A" }
11+
let!(:unique_id) do
12+
Digest::SHA256.hexdigest("#{handler.id_document}-#{organization.id}-#{Rails.application.secrets.secret_key_base}")
13+
end
14+
let(:date) { Date.strptime("1990/11/21", "%Y/%m/%d") }
15+
let(:census_for_user) do
16+
OpenStruct.new(
17+
birth_date: date,
18+
id_document: dni
19+
)
20+
end
21+
let(:handler) do
22+
DibaCensusApiAuthorizationHandler.new(document_type: :dni, id_document: dni, birthdate: date)
23+
end
24+
25+
before do
26+
allow(handler).to receive(:census_for_user).and_return(census_for_user)
27+
end
28+
29+
context "when creating a new impersonation" do
30+
context "when only document_number and birthdate is provided" do
31+
it { is_expected.not_to eq unique_id }
32+
end
33+
34+
context "when an organization_id is provided" do
35+
let(:handler) do
36+
DibaCensusApiAuthorizationHandler.new(document_type: :dni, id_document: dni, birthdate: date, organization_id: organization.id)
37+
end
38+
39+
it { is_expected.to eq unique_id }
40+
end
41+
42+
context "when a user is provided" do
43+
let(:handler) do
44+
DibaCensusApiAuthorizationHandler.new(document_type: :dni, id_document: dni, birthdate: date, user:)
45+
end
46+
47+
it { is_expected.to eq unique_id }
48+
end
49+
end
50+
end

0 commit comments

Comments
 (0)