Class: Shark::RSpec::FakeDoubleOptIn::Request

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/shark/rspec/fake_double_opt_in/request.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.setupObject



11
12
13
14
# File 'lib/shark/rspec/fake_double_opt_in/request.rb', line 11

def self.setup
  instance = self.instance
  instance.stub_requests
end

Instance Method Details

#fake_response(status, body) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/shark/rspec/fake_double_opt_in/request.rb', line 113

def fake_response(status, body)
  {
    headers: {
      content_type: 'application/vnd.api+json'
    },
    status: status,
    body: body.is_a?(String) ? body : body.to_json
  }
end

#hostObject



123
124
125
# File 'lib/shark/rspec/fake_double_opt_in/request.rb', line 123

def host
  Shark.configuration.double_opt_in.site
end

#log_info(message) ⇒ Object



127
128
129
# File 'lib/shark/rspec/fake_double_opt_in/request.rb', line 127

def log_info(message)
  Shark.logger.info "[Shark][DoubleOptInService] #{message}"
end

#stub_requestsObject



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
# File 'lib/shark/rspec/fake_double_opt_in/request.rb', line 16

def stub_requests
  WebMock.stub_request(:post, %r{^#{host}/requests}).to_return do |request|
    log_info "Faking POST request with body: #{request.body}"

    data = JSON.parse(request.body)['data']
    attributes = data['attributes'] || {}

    ObjectCache.instance.create_request(attributes)

    fake_response(200, {
                    data: {
                      id: SecureRandom.uuid,
                      attributes: attributes,
                      type: 'requests'
                    }
                  })
  end

  WebMock.stub_request(:post, %r{^#{host}/executions/.+/verify}).to_return do |request|
    log_info 'Faking POST request'

    verification_token = request.uri.path.split('/').reverse[1]
    object = ObjectCache.instance.find_execution(verification_token)

    if verification_token_invalid?(object)
      fake_response(404, { errors: [] })
    else
      verification_expires_at = object['attributes']['verification_expires_at']
      max_verifications = object['attributes']['max_verifications']
      verifications_count = object['attributes']['verifications_count']

      is_verification_expired = Time.now.to_i > verification_expires_at
      is_number_of_requests_exceeded = max_verifications <= verifications_count

      if is_verification_expired
        errors = [{ code: 'verification_expired' }]
        fake_response(422, { errors: errors })
      elsif is_number_of_requests_exceeded
        errors = [{ code: 'exceeded_number_of_verification_requests' }]
        fake_response(422, { errors: errors })
      else
        fake_response(200, { data: object })
      end
    end
  end

  WebMock.stub_request(:get, %r{^#{host}/executions/.+}).to_return do |request|
    log_info 'Faking GET request'

    verification_token = request.uri.path.split('/').reverse[0]
    object = ObjectCache.instance.find_execution(verification_token)

    if verification_token_invalid?(object)
      fake_response(404, { errors: [] })
    else
      attributes = object['attributes']

      if attributes['verifications_count'].zero?
        fake_response(422, { errors: [{ code: 'requested_unverified_execution' }] })
      else
        fake_response(200, { data: object })
      end
    end
  end

  WebMock.stub_request(:delete, %r{^#{host}/executions/.+}).to_return do |request|
    log_info 'Faking DELETE request'

    verification_token = request.uri.path.split('/').reverse[0]
    object = ObjectCache.instance.find_execution(verification_token)

    if verification_token_invalid?(object)
      fake_response(404, { errors: [] })
    else
      ObjectCache.instance.remove_execution(verification_token)
      fake_response(200, { data: object })
    end
  end
end

#verification_token_invalid?(object) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/shark/rspec/fake_double_opt_in/request.rb', line 96

def verification_token_invalid?(object)
  return true  if object.blank?

  execution_expires_at = object['attributes']['execution_expires_at']
  verification_expires_at = object['attributes']['verification_expires_at']
  verifications_count = object['attributes']['verifications_count']

  is_execution_time_expired = Time.now.to_i > execution_expires_at
  is_verification_expired = Time.now.to_i > verification_expires_at
  has_verification_been_verified = verifications_count.positive?

  return true  if is_execution_time_expired
  return true  if is_verification_expired && !has_verification_been_verified

  false
end