-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpact_helper.rb
138 lines (116 loc) · 4.01 KB
/
pact_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
require "webmock"
require "pact/provider/rspec"
require "plek"
require "gds_api/test_helpers/content_store"
require "gds_api/test_helpers/email_alert_api"
Dir["./spec/support/**/*.rb"].sort.each { |f| require f }
module PactStubHelpers
EMAIL_ADDRESS = "[email protected]".freeze
def stub_cached_attributes(email_verified: true)
oidc_user.update!(
email: EMAIL_ADDRESS,
email_verified:,
)
end
end
def oidc_user
OidcUser.find_or_create_by_sub!("user-id")
end
def url_encode(str)
ERB::Util.url_encode(str)
end
Pact.configure do |config|
config.reports_dir = "spec/reports/pacts"
config.include PactStubHelpers
config.include GdsApi::TestHelpers::ContentStore
config.include GdsApi::TestHelpers::EmailAlertApi
config.include GovukAccountSessionHelper
config.include OidcClientHelper
config.include WebMock::API
config.include WebMock::Matchers
end
Pact.service_provider "Account API" do
honours_pact_with "GDS API Adapters" do
if ENV["PACT_URI"]
pact_uri ENV["PACT_URI"]
else
base_url = "https://govuk-pact-broker-6991351eca05.herokuapp.com"
path = "pacts/provider/#{url_encode(name)}/consumer/#{url_encode(consumer_name)}"
version_modifier = "versions/#{url_encode(ENV.fetch('PACT_CONSUMER_VERSION', 'branch-main'))}"
pact_uri("#{base_url}/#{path}/#{version_modifier}")
end
end
end
Pact.provider_states_for "GDS API Adapters" do
set_up do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.allow_remote_database_url = true
DatabaseCleaner.start
WebMock.enable!
WebMock.reset!
allow(Rails.application.credentials).to receive(:oauth_client_private_key).and_return(nil)
stub_oidc_discovery
stub_token_response
stub_userinfo
account_session = placeholder_govuk_account_session_object(
user_id: oidc_user.sub,
mfa: true,
)
allow(AccountSession).to receive(:deserialise).and_return(account_session)
stub_content_store_has_item(
"/guidance/some-govuk-guidance",
content_item_for_base_path("/guidance/some-govuk-guidance").merge("content_id" => SecureRandom.uuid),
)
end
tear_down do
WebMock.disable!
DatabaseCleaner.clean
end
provider_state "there is a valid OAuth response" do
set_up do
auth_request = AuthRequest.generate!
allow(AuthRequest).to receive(:from_oauth_state).and_return(auth_request)
stub_cached_attributes
end
end
provider_state "there is a valid OAuth response, with the redirect path '/some-arbitrary-path'" do
set_up do
auth_request = AuthRequest.generate!(redirect_path: "/some-arbitrary-path")
allow(AuthRequest).to receive(:from_oauth_state).and_return(auth_request)
stub_cached_attributes
end
end
provider_state "there is a valid OAuth response, with cookie consent 'true'" do
set_up do
auth_request = AuthRequest.generate!(redirect_path: "/some-arbitrary-path")
allow(AuthRequest).to receive(:from_oauth_state).and_return(auth_request)
stub_cached_attributes
oidc_user.update!(cookie_consent: true)
end
end
provider_state "there is a valid user session" do
set_up do
stub_cached_attributes
# rubocop:disable RSpec/AnyInstance
allow_any_instance_of(AccountSession).to receive(:set_remote_attributes)
# rubocop:enable RSpec/AnyInstance
end
end
provider_state "there is a valid user session, with an attribute called 'email'" do
set_up do
stub_cached_attributes
oidc_user.update!(email: "[email protected]")
end
end
provider_state "there is a user with subject identifier 'the-subject-identifier'" do
set_up do
user = FactoryBot.create(:oidc_user, sub: "the-subject-identifier")
stub_request(:get, "#{GdsApi::TestHelpers::EmailAlertApi::EMAIL_ALERT_API_ENDPOINT}/subscribers/govuk-account/#{user.id}").to_return(status: 404)
end
end
provider_state "there is a user with email address '[email protected]'" do
set_up do
FactoryBot.create(:oidc_user, email: "[email protected]")
end
end
end