summaryrefslogtreecommitdiff
path: root/qa/spec/factory/resource/user_spec.rb
blob: d59ee24c758f3267ca251a1760f22e40767c8f38 (plain)
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
# frozen_string_literal: true

describe QA::Resource::User do
  describe "#fabricate_via_api!" do
    response = Struct.new(:code, :body)

    it 'fetches an existing user' do
      existing_users = [
        {
          id: '0',
          name: 'name',
          username: 'name',
          web_url: ''
        }
      ]
      users_response = response.new('200', JSON.dump(existing_users))
      single_user_response = response.new('200', JSON.dump(existing_users.first))

      expect(subject).to receive(:api_get_from).with("/users?username=name").and_return(users_response)
      expect(subject).to receive(:api_get_from).with("/users/0").and_return(single_user_response)

      subject.username = 'name'
      subject.fabricate_via_api!

      expect(subject.api_response).to eq(existing_users.first)
    end

    it 'tries to create a user if it does not exist' do
      expect(subject).to receive(:api_get_from).with("/users?username=foo").and_return(response.new('200', '[]'))
      expect(subject).to receive(:api_post).and_return({ web_url: '' })

      subject.username = 'foo'
      subject.fabricate_via_api!
    end
  end
end