Class: Shark::RSpec::FakeContactService::Request

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.setupObject



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

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

Instance Method Details

#hostObject



151
152
153
# File 'lib/shark/rspec/fake_contact_service/request.rb', line 151

def host
  Shark.configuration.contact_service.site
end

#log_info(message) ⇒ Object



155
156
157
# File 'lib/shark/rspec/fake_contact_service/request.rb', line 155

def log_info(message)
  Shark.logger.info message
end

#query_params_to_object(request_uri) ⇒ Object



159
160
161
162
163
164
165
# File 'lib/shark/rspec/fake_contact_service/request.rb', line 159

def query_params_to_object(request_uri)
  uri = URI.parse(request_uri)
  return {} if uri.query.blank?

  query = uri.query.gsub(/%5B[0-9]%5D/, '%5B%5D')
  Rack::Utils.parse_nested_query(query)
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
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
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/shark/rspec/fake_contact_service/request.rb', line 16

def stub_requests
  cache = FakeContactService::ObjectCache.instance

  WebMock.stub_request(:post, %r{^#{host}/.*}).to_return do |request|
    log_info "[Shark][ContactService] Faking POST request with body: #{request.body}"
    log_info request.uri.to_s

    id = SecureRandom.uuid
    parsed_data = JSON.parse(request.body)['data']
    parsed_data['id'] = id

    cache.add(parsed_data)
    SharkSpec.fake_response(201, data: parsed_data)
  end

  WebMock.stub_request(:get, %r{^#{host}/.*$}).to_return do |request|
    log_info '[Shark][ContactService] Faking GET request'
    log_info request.uri.to_s

    type = request.uri.path.split('/')[-1]
    params = query_params_to_object(request.uri)

    objects = if %w[contacts accounts].include?(type) && params['filter'].present?
                cache.search_objects(type, params)
              elsif %w[activities].include?(type) && params['filter'].present?
                cache.objects_contain(type, params)
              else
                cache.objects.select do |object|
                  object['type'] == type
                end
              end

    SharkSpec.fake_response(200, data: objects)
  end

  WebMock.stub_request(:get, %r{^#{host}/.+/.+}).to_return do |request|
    log_info '[Shark][ContactService] Faking GET request with ID'
    log_info request.uri.to_s

    type = request.uri.path.split('/')[-2]
    id = request.uri.path.split('/')[-1]
    query = request.uri.query_values

    object = cache.find(type, id)

    if object.present?
      body = { data: object }

      if query && query['include']
        relation_name = query['include']
        included_objects = cache.included_resources(object, query)

        if included_objects.empty?
          object['relationships'] ||= { relation_name => { data: [] } }
        end
        body = {
          data: object,
          included: included_objects
        }
      end

      SharkSpec.fake_response(200, body)
    else
      SharkSpec.fake_response(404, errors: [])
    end
  end

  WebMock.stub_request(:get, %r{^#{host}/groups/.+/memberships}).to_return do |request|
    log_info '[Shark][ContactService] Faking GET memberships request'
    log_info request.uri.to_s

    group_id = request.uri.path.split('/')[-2]
    contact_id = query_params_to_object(request.uri)['filter']['contact_id']

    group = cache.find('groups', group_id)

    if group.present?
      contacts = group.dig('relationships', 'contacts', 'data')

      if contacts.present?
        if contacts.detect { |c| c['type'] == 'contacts' && c['id'] == contact_id }
          next SharkSpec.fake_response(200, {})
        end
      end
    end

    SharkSpec.fake_response(404, errors: [])
  end

  WebMock.stub_request(:patch, %r{^#{host}/.*/.+}).to_return do |request|
    log_info "[Shark][ContactService] Faking PATCH request with body: #{request.body}"
    log_info request.uri.to_s

    type = request.uri.path.split('/')[-2]
    id = request.uri.path.split('/')[-1]
    parsed_data = JSON.parse(request.body)['data']

    object = cache.find(type, id)

    if object.present?
      (parsed_data['attributes'] || {}).each do |key, value|
        object['attributes'] = {} if object['attributes'].blank?
        object['attributes'][key] = value
      end

      (parsed_data['relationships'] || {}).each do |key, value|
        object['relationships'] = {} if object['relationships'].blank?
        object['relationships'][key] = value
      end

      SharkSpec.fake_response(200, data: object)
    else
      SharkSpec.fake_response(404, errors: [])
    end
  end

  WebMock.stub_request(:delete, %r{^#{host}/.*/.+}).to_return do |request|
    log_info '[Shark][ContactService] Faking DELETE request'
    log_info request.uri.to_s

    type = request.uri.path.split('/')[-2]
    id = request.uri.path.split('/')[-1]

    object = cache.find(type, id)

    if object.present?
      cache.objects.delete(object)

      SharkSpec.fake_response(204, nil)
    else
      SharkSpec.fake_response(404, errors: [])
    end
  end
end