summaryrefslogtreecommitdiff
path: root/spec/requests/api/labels_spec.rb
blob: ee9088933a1cf609fda1864779d8be4bb284c0d6 (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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
require 'spec_helper'

describe API::API, api: true  do
  include ApiHelpers

  let(:user) { create(:user) }
  let(:project) { create(:project, creator_id: user.id, namespace: user.namespace) }
  let!(:label1) { create(:label, title: 'label1', project: project) }

  before do
    project.team << [user, :master]
  end


  describe 'GET /projects/:id/labels' do
    it 'should return project labels' do
      get api("/projects/#{project.id}/labels", user)
      response.status.should == 200
      json_response.should be_an Array
      json_response.size.should == 1
      json_response.first['name'].should == label1.name
    end
  end

  describe 'POST /projects/:id/labels' do
    it 'should return created label' do
      post api("/projects/#{project.id}/labels", user),
           name: 'Foo',
           color: '#FFAABB'
      response.status.should == 201
      json_response['name'].should == 'Foo'
      json_response['color'].should == '#FFAABB'
    end

    it 'should return a 400 bad request if name not given' do
      post api("/projects/#{project.id}/labels", user), color: '#FFAABB'
      response.status.should == 400
    end

    it 'should return a 400 bad request if color not given' do
      post api("/projects/#{project.id}/labels", user), name: 'Foobar'
      response.status.should == 400
    end

    it 'should return 400 for invalid color' do
      post api("/projects/#{project.id}/labels", user),
           name: 'Foo',
           color: '#FFAA'
      response.status.should == 400
      json_response['message'].should == 'Color is invalid'
    end

    it 'should return 400 for too long color code' do
      post api("/projects/#{project.id}/labels", user),
           name: 'Foo',
           color: '#FFAAFFFF'
      response.status.should == 400
      json_response['message'].should == 'Color is invalid'
    end

    it 'should return 400 for invalid name' do
      post api("/projects/#{project.id}/labels", user),
           name: '?',
           color: '#FFAABB'
      response.status.should == 400
      json_response['message'].should == 'Title is invalid'
    end

    it 'should return 409 if label already exists' do
      post api("/projects/#{project.id}/labels", user),
           name: 'label1',
           color: '#FFAABB'
      response.status.should == 409
      json_response['message'].should == 'Label already exists'
    end
  end

  describe 'DELETE /projects/:id/labels' do
    it 'should return 200 for existing label' do
      delete api("/projects/#{project.id}/labels", user), name: 'label1'
      response.status.should == 200
    end

    it 'should return 404 for non existing label' do
      delete api("/projects/#{project.id}/labels", user), name: 'label2'
      response.status.should == 404
      json_response['message'].should == 'Label not found'
    end

    it 'should return 400 for wrong parameters' do
      delete api("/projects/#{project.id}/labels", user)
      response.status.should == 400
    end
  end

  describe 'PUT /projects/:id/labels' do
    it 'should return 200 if name and colors are changed' do
      put api("/projects/#{project.id}/labels", user),
          name: 'label1',
          new_name: 'New Label',
          color: '#FFFFFF'
      response.status.should == 200
      json_response['name'].should == 'New Label'
      json_response['color'].should == '#FFFFFF'
    end

    it 'should return 200 if name is changed' do
      put api("/projects/#{project.id}/labels", user),
          name: 'label1',
          new_name: 'New Label'
      response.status.should == 200
      json_response['name'].should == 'New Label'
      json_response['color'].should == label1.color
    end

    it 'should return 200 if colors is changed' do
      put api("/projects/#{project.id}/labels", user),
          name: 'label1',
          color: '#FFFFFF'
      response.status.should == 200
      json_response['name'].should == label1.name
      json_response['color'].should == '#FFFFFF'
    end

    it 'should return 404 if label does not exist' do
      put api("/projects/#{project.id}/labels", user),
          name: 'label2',
          new_name: 'label3'
      response.status.should == 404
    end

    it 'should return 400 if no label name given' do
      put api("/projects/#{project.id}/labels", user), new_name: 'label2'
      response.status.should == 400
    end

    it 'should return 400 if no new parameters given' do
      put api("/projects/#{project.id}/labels", user), name: 'label1'
      response.status.should == 400
    end

    it 'should return 400 for invalid name' do
      put api("/projects/#{project.id}/labels", user),
          name: 'label1',
          new_name: '?',
          color: '#FFFFFF'
      response.status.should == 400
      json_response['message'].should == 'Title is invalid'
    end

    it 'should return 400 for invalid name' do
      put api("/projects/#{project.id}/labels", user),
          name: 'label1',
          color: '#FF'
      response.status.should == 400
      json_response['message'].should == 'Color is invalid'
    end

    it 'should return 400 for too long color code' do
      post api("/projects/#{project.id}/labels", user),
           name: 'Foo',
           color: '#FFAAFFFF'
      response.status.should == 400
      json_response['message'].should == 'Color is invalid'
    end
  end
end