Module: ClearElection::Rspec::Helpers

Defined in:
lib/clear-election-sdk/rspec.rb

Instance Method Summary collapse

Instance Method Details

#my_agent_uriObject



55
56
57
# File 'lib/clear-election-sdk/rspec.rb', line 55

def my_agent_uri
  @my_agent_uri ||= ClearElection::Factory.agent_uri(Rails.root.basename)
end

#setup_my_agent_uriObject

For use in an agent: create a URI that will act in rspec as if it’s URI at which the app was called



48
49
50
51
52
53
# File 'lib/clear-election-sdk/rspec.rb', line 48

def setup_my_agent_uri
  host! URI(my_agent_uri).host
  allow_any_instance_of(ActionDispatch::Request).to receive(:original_url) { |request|
    request.base_url + URI(my_agent_uri).path + request.original_fullpath
  }
end

#stub_election_access_token(election_uri:, election: nil, accessToken: nil, demographic: nil, valid: true) ⇒ Object

creates a webmock stub for signin with an access token



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/clear-election-sdk/rspec.rb', line 34

def stub_election_access_token(election_uri:, election: nil, accessToken: nil, demographic: nil, valid: true)
  accessToken ||= SecureRandom.hex(10)
  if valid
    result = { status: 200, body: JSON.generate(demographic: demographic) }
  else
    result = { status: 403 }
  end
  election ||= ClearElection.read(election_uri)
  stub_request(:post, election..uri + "redeem").with(body: {election: election_uri, accessToken: accessToken}).to_return result
  accessToken
end

#stub_election_uri(election: nil, election_uri: nil, booth: nil, signin: nil, pollsOpen: nil, pollsClose: nil, valid: true) ⇒ Object

creates a webmock stub request for an election_uri to return an election



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/clear-election-sdk/rspec.rb', line 21

def stub_election_uri(election: nil, election_uri: nil, booth: nil, signin: nil, pollsOpen: nil, pollsClose: nil, valid: true)
  election_uri ||= ClearElection::Factory.election_uri
  if valid
    election ||= ClearElection::Factory.election(booth: booth, signin: , pollsOpen: pollsOpen, pollsClose: pollsClose)
    result = { body: JSON.generate(election.as_json) }
  else
    result = { status: 404 }
  end
  stub_request(:get, election_uri).to_return result
  election_uri
end