File: user_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (108 lines) | stat: -rw-r--r-- 3,066 bytes parent folder | download
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
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
# frozen_string_literal: true

RSpec.describe QA::Resource::User do
  subject(:user) { described_class }

  describe "#fabricate_via_api!" do
    let(:address) { "https://example.com" }
    let(:token) { "foo" }
    let(:request_args) { { url: "#{address}/api/v4/user?private_token=#{token}", verify_ssl: false } }

    let(:body) do
      {
        id: '0',
        name: 'name',
        username: 'name',
        web_url: '',
        email: "test@email.com"
      }
    end

    def response(resp_body, code: 200)
      instance_double(RestClient::Response, code: code, body: resp_body.to_json)
    end

    def url(path)
      "#{path}private_token=#{token}"
    end

    before do
      allow(QA::Runtime::Scenario).to receive(:gitlab_address).and_return(address)
      allow(QA::Runtime::UserStore).to receive(:admin_api_client).and_return(
        QA::Runtime::API::Client.new(personal_access_token: token)
      )

      allow(RestClient::Request).to receive(:execute)
        .with({ method: :get, url: "#{address}/api/v4/#{url('user?')}", verify_ssl: false })
        .and_return(response(body))
    end

    context "with existing user" do
      it "return existing user" do
        resource = user.fabricate_via_api! do |u|
          u.username = 'name'
        end

        expect(resource.id).to eq("0")
      end
    end

    context "with token no belonging to user" do
      let(:existing_user) do
        {
          id: '1',
          name: 'name',
          username: 'test',
          web_url: '',
          email: "test@email.com"
        }
      end

      before do
        allow(RestClient::Request).to receive(:execute)
          .with(hash_including(method: :get, url: "#{address}/api/v4/#{url('users?username=test&')}"))
          .and_return(response([existing_user]))
        allow(RestClient::Request).to receive(:execute)
          .with(hash_including(method: :get, url: "#{address}/api/v4/#{url('users/1?')}"))
          .and_return(response(existing_user))
      end

      it "fetches existing user via id lookup" do
        resource = user.fabricate_via_api! do |u|
          u.username = 'test'
        end

        expect(resource.id).to eq("1")
      end
    end

    context "without existing user" do
      let(:existing_user) do
        {
          id: '1',
          name: 'name',
          username: 'test',
          web_url: '',
          email: "test@email.com"
        }
      end

      before do
        allow(RestClient::Request).to receive(:execute)
          .with(hash_including(method: :get, url: "#{address}/api/v4/#{url('users?username=test&')}"))
          .and_return(response([]))
        allow(RestClient::Request).to receive(:execute)
          .with(hash_including(method: :post, url: "#{address}/api/v4/#{url('users?')}"))
          .and_return(response(existing_user, code: 201))
      end

      it "fetches existing user via id lookup" do
        resource = user.fabricate_via_api! do |u|
          u.username = 'test'
        end

        expect(resource.id).to eq("1")
      end
    end
  end
end