summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/requests/api/custom_attributes_shared_examples.rb
blob: c9302f7b750fc74ec4edb87e8640a4424b78f29e (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
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
shared_examples 'custom attributes endpoints' do |attributable_name|
  let!(:custom_attribute1) { attributable.custom_attributes.create key: 'foo', value: 'foo' }
  let!(:custom_attribute2) { attributable.custom_attributes.create key: 'bar', value: 'bar' }

  describe "GET /#{attributable_name} with custom attributes filter" do
    let!(:other_attributable) { create attributable.class.name.underscore }

    context 'with an unauthorized user' do
      it 'does not filter by custom attributes' do
        get api("/#{attributable_name}", user), custom_attributes: { foo: 'foo', bar: 'bar' }

        expect(response).to have_http_status(200)
        expect(json_response.size).to be 2
      end
    end

    it 'filters by custom attributes' do
      get api("/#{attributable_name}", admin), custom_attributes: { foo: 'foo', bar: 'bar' }

      expect(response).to have_http_status(200)
      expect(json_response.size).to be 1
      expect(json_response.first['id']).to eq attributable.id
    end
  end

  describe "GET /#{attributable_name}/:id/custom_attributes" do
    context 'with an unauthorized user' do
      subject { get api("/#{attributable_name}/#{attributable.id}/custom_attributes", user) }

      it_behaves_like 'an unauthorized API user'
    end

    it 'returns all custom attributes' do
      get api("/#{attributable_name}/#{attributable.id}/custom_attributes", admin)

      expect(response).to have_http_status(200)
      expect(json_response).to contain_exactly(
        { 'key' => 'foo', 'value' => 'foo' },
        { 'key' => 'bar', 'value' => 'bar' }
      )
    end
  end

  describe "GET /#{attributable_name}/:id/custom_attributes/:key" do
    context 'with an unauthorized user' do
      subject { get api("/#{attributable_name}/#{attributable.id}/custom_attributes/foo", user) }

      it_behaves_like 'an unauthorized API user'
    end

    it 'returns a single custom attribute' do
      get api("/#{attributable_name}/#{attributable.id}/custom_attributes/foo", admin)

      expect(response).to have_http_status(200)
      expect(json_response).to eq({ 'key' => 'foo', 'value' => 'foo' })
    end
  end

  describe "PUT /#{attributable_name}/:id/custom_attributes/:key" do
    context 'with an unauthorized user' do
      subject { put api("/#{attributable_name}/#{attributable.id}/custom_attributes/foo", user), value: 'new' }

      it_behaves_like 'an unauthorized API user'
    end

    it 'creates a new custom attribute' do
      expect do
        put api("/#{attributable_name}/#{attributable.id}/custom_attributes/new", admin), value: 'new'
      end.to change { attributable.custom_attributes.count }.by(1)

      expect(response).to have_http_status(200)
      expect(json_response).to eq({ 'key' => 'new', 'value' => 'new' })
      expect(attributable.custom_attributes.find_by(key: 'new').value).to eq 'new'
    end

    it 'updates an existing custom attribute' do
      expect do
        put api("/#{attributable_name}/#{attributable.id}/custom_attributes/foo", admin), value: 'new'
      end.not_to change { attributable.custom_attributes.count }

      expect(response).to have_http_status(200)
      expect(json_response).to eq({ 'key' => 'foo', 'value' => 'new' })
      expect(custom_attribute1.reload.value).to eq 'new'
    end
  end

  describe "DELETE /#{attributable_name}/:id/custom_attributes/:key" do
    context 'with an unauthorized user' do
      subject { delete api("/#{attributable_name}/#{attributable.id}/custom_attributes/foo", user) }

      it_behaves_like 'an unauthorized API user'
    end

    it 'deletes an existing custom attribute' do
      expect do
        delete api("/#{attributable_name}/#{attributable.id}/custom_attributes/foo", admin)
      end.to change { attributable.custom_attributes.count }.by(-1)

      expect(response).to have_http_status(204)
      expect(attributable.custom_attributes.find_by(key: 'foo')).to be_nil
    end
  end
end