allow anonymous users to GET api routes (#842)

* make map methods use ActiveRecord relations so they don't error on pundit

* test for logged out maps GET api

* open up GET routes on maps/topics/synapses and update api docs
This commit is contained in:
Devin Howard 2016-10-26 08:37:23 +08:00 committed by GitHub
parent ed89f80f49
commit 8a95262f2c
8 changed files with 18 additions and 14 deletions

View file

@ -41,11 +41,11 @@ class Map < ApplicationRecord
end end
def contributors def contributors
mappings.map(&:user).uniq User.where(id: mappings.map(&:user_id).uniq)
end end
def editors def editors
collaborators + [user] User.where(id: user_id).or(User.where(id: collaborators))
end end
def topic_count def topic_count
@ -87,7 +87,7 @@ class Map < ApplicationRecord
end end
def starred_by_user?(user) def starred_by_user?(user)
user.stars.where(map: self).exists? user&.stars&.where(map: self)&.exists? || false # return false, not nil
end end
def as_json(_options = {}) def as_json(_options = {})
@ -114,9 +114,8 @@ class Map < ApplicationRecord
def add_new_collaborators(user_ids) def add_new_collaborators(user_ids)
users = User.where(id: user_ids) users = User.where(id: user_ids)
current_collaborators = collaborators + [user]
added = users.map do |new_user| added = users.map do |new_user|
next nil if current_collaborators.include?(new_user) next nil if editors.include?(new_user)
UserMap.create(user_id: new_user.id, map_id: id) UserMap.create(user_id: new_user.id, map_id: id)
new_user.id new_user.id
end end
@ -124,8 +123,7 @@ class Map < ApplicationRecord
end end
def remove_old_collaborators(user_ids) def remove_old_collaborators(user_ids)
current_collaborators = collaborators + [user] removed = editors.map(&:id).map do |old_user_id|
removed = current_collaborators.map(&:id).map do |old_user_id|
next nil if user_ids.include?(old_user_id) next nil if user_ids.include?(old_user_id)
user_maps.where(user_id: old_user_id).find_each(&:destroy) user_maps.where(user_id: old_user_id).find_each(&:destroy)
access_requests.where(user_id: old_user_id).find_each(&:destroy) access_requests.where(user_id: old_user_id).find_each(&:destroy)

View file

@ -12,7 +12,7 @@ class TopicPolicy < ApplicationPolicy
end end
def index? def index?
user.present? true
end end
def create? def create?

View file

@ -1,6 +1,7 @@
#type: collection #type: collection
get: get:
is: [ searchable: { searchFields: "name, desc" }, embeddable: { embedFields: "user,topics,synapses,mappings,contributors,collaborators" }, orderable, pageable ] is: [ searchable: { searchFields: "name, desc" }, embeddable: { embedFields: "user,topics,synapses,mappings,contributors,collaborators" }, orderable, pageable ]
securedBy: [ null, cookie, token, oauth_2_0 ]
responses: responses:
200: 200:
body: body:
@ -31,6 +32,7 @@ post:
#type: item #type: item
get: get:
is: [ embeddable: { embedFields: "user,topics,synapses,mappings,contributors,collaborators" } ] is: [ embeddable: { embedFields: "user,topics,synapses,mappings,contributors,collaborators" } ]
securedBy: [ null, cookie, token, oauth_2_0 ]
responses: responses:
200: 200:
body: body:

View file

@ -1,6 +1,7 @@
#type: collection #type: collection
get: get:
is: [ searchable: { searchFields: "desc" }, embeddable: { embedFields: "topic1,topic2,user" }, orderable, pageable ] is: [ searchable: { searchFields: "desc" }, embeddable: { embedFields: "topic1,topic2,user" }, orderable, pageable ]
securedBy: [ null, cookie, token, oauth_2_0 ]
responses: responses:
200: 200:
body: body:
@ -33,6 +34,7 @@ post:
#type: item #type: item
get: get:
is: [ embeddable: { embedFields: "topic1,topic2,user" } ] is: [ embeddable: { embedFields: "topic1,topic2,user" } ]
securedBy: [ null, cookie, token, oauth_2_0 ]
responses: responses:
200: 200:
body: body:

View file

@ -1,6 +1,7 @@
#type: collection #type: collection
get: get:
is: [ searchable: { searchFields: "name, desc, link" }, embeddable: { embedFields: "user,metacode" }, orderable, pageable ] is: [ searchable: { searchFields: "name, desc, link" }, embeddable: { embedFields: "user,metacode" }, orderable, pageable ]
securedBy: [ null, cookie, token, oauth_2_0 ]
responses: responses:
200: 200:
body: body:
@ -30,6 +31,7 @@ post:
#type: item #type: item
get: get:
is: [ embeddable: { embedFields: "user,metacode" } ] is: [ embeddable: { embedFields: "user,metacode" } ]
securedBy: [ null, cookie, token, oauth_2_0 ]
responses: responses:
200: 200:
body: body:

View file

@ -8,7 +8,7 @@ RSpec.describe 'maps API', type: :request do
it 'GET /api/v2/maps' do it 'GET /api/v2/maps' do
create_list(:map, 5) create_list(:map, 5)
get '/api/v2/maps', params: { access_token: token } get '/api/v2/maps'
expect(response).to have_http_status(:success) expect(response).to have_http_status(:success)
expect(response).to match_json_schema(:maps) expect(response).to match_json_schema(:maps)
@ -16,7 +16,7 @@ RSpec.describe 'maps API', type: :request do
end end
it 'GET /api/v2/maps/:id' do it 'GET /api/v2/maps/:id' do
get "/api/v2/maps/#{map.id}", params: { access_token: token } get "/api/v2/maps/#{map.id}"
expect(response).to have_http_status(:success) expect(response).to have_http_status(:success)
expect(response).to match_json_schema(:map) expect(response).to match_json_schema(:map)

View file

@ -8,7 +8,7 @@ RSpec.describe 'synapses API', type: :request do
it 'GET /api/v2/synapses' do it 'GET /api/v2/synapses' do
create_list(:synapse, 5) create_list(:synapse, 5)
get '/api/v2/synapses', params: { access_token: token } get '/api/v2/synapses'
expect(response).to have_http_status(:success) expect(response).to have_http_status(:success)
expect(response).to match_json_schema(:synapses) expect(response).to match_json_schema(:synapses)

View file

@ -8,7 +8,7 @@ RSpec.describe 'topics API', type: :request do
it 'GET /api/v2/topics' do it 'GET /api/v2/topics' do
create_list(:topic, 5) create_list(:topic, 5)
get '/api/v2/topics', params: { access_token: token } get '/api/v2/topics'
expect(response).to have_http_status(:success) expect(response).to have_http_status(:success)
expect(response).to match_json_schema(:topics) expect(response).to match_json_schema(:topics)
@ -16,7 +16,7 @@ RSpec.describe 'topics API', type: :request do
end end
it 'GET /api/v2/topics/:id' do it 'GET /api/v2/topics/:id' do
get "/api/v2/topics/#{topic.id}", params: { access_token: token } get "/api/v2/topics/#{topic.id}"
expect(response).to have_http_status(:success) expect(response).to have_http_status(:success)