summaryrefslogtreecommitdiff
path: root/spec/support
diff options
context:
space:
mode:
authorSean McGivern <sean@gitlab.com>2018-08-22 13:10:54 +0100
committerSean McGivern <sean@gitlab.com>2018-08-22 14:17:29 +0100
commitaff7dccc1f13e86b44dfa1530c6b5068dbb18f00 (patch)
tree0193754c17b7023499ad4d1cd438cb287547793d /spec/support
parentb63ed7cff664bc1ee0bf70912fffd4814f757079 (diff)
downloadgitlab-ce-aff7dccc1f13e86b44dfa1530c6b5068dbb18f00.tar.gz
Use policies to determine if attributes can be set in the API
This is more idiomatic than checking membership explicitly.
Diffstat (limited to 'spec/support')
-rw-r--r--spec/support/shared_examples/requests/api/notes.rb82
1 files changed, 72 insertions, 10 deletions
diff --git a/spec/support/shared_examples/requests/api/notes.rb b/spec/support/shared_examples/requests/api/notes.rb
index 1b563021244..0e20dfe0725 100644
--- a/spec/support/shared_examples/requests/api/notes.rb
+++ b/spec/support/shared_examples/requests/api/notes.rb
@@ -111,17 +111,79 @@ shared_examples 'noteable API' do |parent_type, noteable_type, id_name|
post api("/#{parent_type}/#{parent.id}/#{noteable_type}/#{noteable[id_name]}/notes", user), body: 'hi!'
end
- context 'when an admin or owner makes the request' do
- it 'accepts the creation date to be set' do
- creation_time = 2.weeks.ago
- post api("/#{parent_type}/#{parent.id}/#{noteable_type}/#{noteable[id_name]}/notes", user),
- body: 'hi!', created_at: creation_time
+ context 'setting created_at' do
+ let(:creation_time) { 2.weeks.ago }
+ let(:params) { { body: 'hi!', created_at: creation_time } }
+
+ context 'by an admin' do
+ it 'sets the creation time on the new note' do
+ admin = create(:admin)
+ post api("/#{parent_type}/#{parent.id}/#{noteable_type}/#{noteable[id_name]}/notes", admin), params
+
+ expect(response).to have_gitlab_http_status(201)
+ expect(json_response['body']).to eq('hi!')
+ expect(json_response['author']['username']).to eq(admin.username)
+ expect(Time.parse(json_response['created_at'])).to be_like_time(creation_time)
+ expect(Time.parse(json_response['updated_at'])).to be_like_time(creation_time)
+ end
+ end
- expect(response).to have_gitlab_http_status(201)
- expect(json_response['body']).to eq('hi!')
- expect(json_response['author']['username']).to eq(user.username)
- expect(Time.parse(json_response['created_at'])).to be_like_time(creation_time)
- expect(Time.parse(json_response['updated_at'])).to be_like_time(creation_time)
+ if parent_type == 'projects'
+ context 'by a project owner' do
+ it 'sets the creation time on the new note' do
+ post api("/#{parent_type}/#{parent.id}/#{noteable_type}/#{noteable[id_name]}/notes", user), params
+
+ expect(response).to have_gitlab_http_status(201)
+ expect(json_response['body']).to eq('hi!')
+ expect(json_response['author']['username']).to eq(user.username)
+ expect(Time.parse(json_response['created_at'])).to be_like_time(creation_time)
+ expect(Time.parse(json_response['updated_at'])).to be_like_time(creation_time)
+ end
+ end
+
+ context 'by a group owner' do
+ it 'sets the creation time on the new note' do
+ user2 = create(:user)
+ group = create(:group)
+ group.add_owner(user2)
+ parent.update!(namespace: group)
+ user2.refresh_authorized_projects
+
+ post api("/#{parent_type}/#{parent.id}/#{noteable_type}/#{noteable[id_name]}/notes", user2), params
+
+ expect(response).to have_gitlab_http_status(201)
+ expect(json_response['body']).to eq('hi!')
+ expect(json_response['author']['username']).to eq(user2.username)
+ expect(Time.parse(json_response['created_at'])).to be_like_time(creation_time)
+ expect(Time.parse(json_response['updated_at'])).to be_like_time(creation_time)
+ end
+ end
+ elsif parent_type == 'groups'
+ context 'by a group owner' do
+ it 'sets the creation time on the new note' do
+ post api("/#{parent_type}/#{parent.id}/#{noteable_type}/#{noteable[id_name]}/notes", user), params
+
+ expect(response).to have_gitlab_http_status(201)
+ expect(json_response['body']).to eq('hi!')
+ expect(json_response['author']['username']).to eq(user.username)
+ expect(Time.parse(json_response['created_at'])).to be_like_time(creation_time)
+ expect(Time.parse(json_response['updated_at'])).to be_like_time(creation_time)
+ end
+ end
+ end
+
+ context 'by another user' do
+ it 'ignores the given creation time' do
+ user2 = create(:user)
+ parent.add_developer(user2)
+ post api("/#{parent_type}/#{parent.id}/#{noteable_type}/#{noteable[id_name]}/notes", user2), params
+
+ expect(response).to have_gitlab_http_status(201)
+ expect(json_response['body']).to eq('hi!')
+ expect(json_response['author']['username']).to eq(user2.username)
+ expect(Time.parse(json_response['created_at'])).not_to be_like_time(creation_time)
+ expect(Time.parse(json_response['updated_at'])).not_to be_like_time(creation_time)
+ end
end
end