summaryrefslogtreecommitdiff
path: root/spec/requests/api/users_preferences_spec.rb
blob: db03786ed2a25bfc2be5da134dd5ad237e145330 (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
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe API::Users do
  let_it_be(:user) { create(:user) }

  describe 'PUT /user/preferences/' do
    context "with correct attributes and a logged in user" do
      it 'returns a success status and the value has been changed' do
        put api("/user/preferences", user), params: { view_diffs_file_by_file: true }

        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response['view_diffs_file_by_file']).to eq(true)
        expect(user.reload.view_diffs_file_by_file).to be_truthy
      end
    end

    context "missing a preference" do
      it 'returns a bad request status' do
        put api("/user/preferences", user), params: {}

        expect(response).to have_gitlab_http_status(:bad_request)
      end
    end

    context "without a logged in user" do
      it 'returns an unauthorized status' do
        put api("/user/preferences"), params: { view_diffs_file_by_file: true }

        expect(response).to have_gitlab_http_status(:unauthorized)
      end
    end

    context "with an unsupported preference" do
      it 'returns a bad parameter' do
        put api("/user/preferences", user), params: { jawn: true }

        expect(response).to have_gitlab_http_status(:bad_request)
      end
    end

    context "with an unsupported value" do
      it 'returns a bad parameter' do
        put api("/user/preferences", user), params: { view_diffs_file_by_file: 3 }

        expect(response).to have_gitlab_http_status(:bad_request)
      end
    end

    context "with an update service failure" do
      it 'returns a bad request' do
        bad_service = double("Failed Service", success?: false)

        allow_next_instance_of(::UserPreferences::UpdateService) do |instance|
          allow(instance).to receive(:execute).and_return(bad_service)
        end

        put api("/user/preferences", user), params: { view_diffs_file_by_file: true }

        expect(response).to have_gitlab_http_status(:bad_request)
      end
    end
  end
end