Module: Webhookdb::SpecHelpers::Service

Defined in:
lib/webhookdb/spec_helpers/service.rb

Defined Under Namespace

Modules: WebhookdbTestMethods Classes: FakeSentryScope, HaveJSONBodyMatcher, HaveStatusMatcher, Rewindable

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.have_json_body(expected_type = nil) ⇒ Object

Create a new matcher that will expect the response to have a JSON body of the expected_type. If expected_type is omitted, any JSON body will be sufficient for a match.



393
394
395
# File 'lib/webhookdb/spec_helpers/service.rb', line 393

module_function def have_json_body(expected_type=nil)
  return HaveJSONBodyMatcher.new(expected_type)
end

.have_status(expected_status) ⇒ Object

Matcher that will have a failure message if the response does not have the expected status.

expect( last_response ).to have_status( 200 )


370
371
372
# File 'lib/webhookdb/spec_helpers/service.rb', line 370

module_function def have_status(expected_status)
  return HaveStatusMatcher.new(expected_status)
end

.included(context) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/webhookdb/spec_helpers/service.rb', line 13

def self.included(context)
  context.include(WebhookdbTestMethods)

  ::Warden.test_mode!

  super

  context.after(:each) { Warden.test_reset! }
end

.last_response_json_body(expected_type = nil) ⇒ Object

Parse the body of the last response and return it as a Ruby object.



398
399
400
401
402
# File 'lib/webhookdb/spec_helpers/service.rb', line 398

module_function def last_response_json_body(expected_type=nil)
  matcher = have_json_body(expected_type)
  expect(last_response).to(matcher)
  return matcher.parsed_response_body
end

Instance Method Details

#fake_request(input: "", env: {}) ⇒ Object



58
59
60
61
# File 'lib/webhookdb/spec_helpers/service.rb', line 58

def fake_request(input: "", env: {})
  req = Rack::Request.new(env.merge({"rack.input" => Rewindable.new(input)}))
  return req
end

#impersonate(admin: nil, target: nil) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/webhookdb/spec_helpers/service.rb', line 42

def impersonate(admin: nil, target: nil)
  admin ||= Webhookdb::Fixtures.customer.admin.create
  target ||= Webhookdb::Fixtures.customer.create
  Warden.on_next_request do |proxy|
    proxy.set_user(admin, event: :authentication, scope: :admin)
    proxy.set_user(target, event: :authentication, scope: :customer)
    Webhookdb::Service::Auth::Impersonation.new(proxy).on(target)
  end
end

#last_session_idObject



23
24
25
26
27
# File 'lib/webhookdb/spec_helpers/service.rb', line 23

def last_session_id
  (set_cookie = last_response["Set-Cookie"]) or return nil
  (session = Webhookdb::Service.decode_cookie(set_cookie)) or return nil
  return session["session_id"]
end

#login_as(customer, opts = nil) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/webhookdb/spec_helpers/service.rb', line 29

def (customer, opts=nil)
  opts ||= {scope: :customer}
  Warden.on_next_request do |proxy|
    opts[:event] ||= :authentication
    proxy.set_user(customer, opts)
  end
end

#login_as_admin(customer, opts = {}) ⇒ Object



37
38
39
40
# File 'lib/webhookdb/spec_helpers/service.rb', line 37

def (customer, opts={})
  (customer, opts.merge(scope: :customer))
  (customer, opts.merge(scope: :admin))
end

#logout(*scopes) ⇒ Object



52
53
54
55
56
# File 'lib/webhookdb/spec_helpers/service.rb', line 52

def logout(*scopes)
  Warden.on_next_request do |proxy|
    proxy.logout(*scopes)
  end
end