2016-12-13 03:28:10 +00:00
|
|
|
# frozen_string_literal: true
|
2017-11-06 16:15:18 +00:00
|
|
|
|
2016-12-13 03:28:10 +00:00
|
|
|
# t frozen_string_literal: true
|
2016-09-21 17:22:40 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe 'maps API', type: :request do
|
|
|
|
let(:user) { create(:user, admin: true) }
|
|
|
|
let(:token) { create(:token, user: user).token }
|
2016-12-18 21:17:51 +00:00
|
|
|
let(:source) { create(:map, user: user) }
|
|
|
|
let(:map) { create(:map, user: user, source: source) }
|
2016-09-21 17:22:40 +00:00
|
|
|
|
2016-10-29 14:07:27 +00:00
|
|
|
describe 'GET /api/v2/maps' do
|
|
|
|
it 'returns all maps' do
|
|
|
|
create_list(:map, 5)
|
|
|
|
get '/api/v2/maps'
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(response).to match_json_schema(:maps)
|
|
|
|
expect(JSON.parse(response.body)['data'].count).to eq 5
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'filters by user id' do
|
|
|
|
create(:map, user_id: 1)
|
|
|
|
create(:map, user_id: 2)
|
|
|
|
create(:map, user_id: 2, permission: :private)
|
|
|
|
get '/api/v2/maps', params: { user_id: 2 }
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(JSON.parse(response.body)['data'].count).to eq 1
|
|
|
|
expect(JSON.parse(response.body)['data'][0]['user_id']).to eq 2
|
|
|
|
end
|
2016-09-21 17:22:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'GET /api/v2/maps/:id' do
|
2016-10-26 00:37:23 +00:00
|
|
|
get "/api/v2/maps/#{map.id}"
|
2016-09-21 17:22:40 +00:00
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(response).to match_json_schema(:map)
|
|
|
|
expect(JSON.parse(response.body)['data']['id']).to eq map.id
|
|
|
|
end
|
2016-12-13 03:28:10 +00:00
|
|
|
|
2016-09-21 17:22:40 +00:00
|
|
|
it 'POST /api/v2/maps' do
|
|
|
|
post '/api/v2/maps', params: { map: map.attributes, access_token: token }
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(response).to match_json_schema(:map)
|
2016-12-18 21:17:51 +00:00
|
|
|
expect(Map.count).to eq 3
|
2016-09-21 17:22:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'PATCH /api/v2/maps/:id' do
|
|
|
|
patch "/api/v2/maps/#{map.id}", params: { map: map.attributes, access_token: token }
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(response).to match_json_schema(:map)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'DELETE /api/v2/maps/:id' do
|
|
|
|
delete "/api/v2/maps/#{map.id}", params: { access_token: token }
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:no_content)
|
2016-12-18 21:17:51 +00:00
|
|
|
expect(Map.count).to eq 1
|
2016-09-21 17:22:40 +00:00
|
|
|
end
|
|
|
|
|
2016-10-05 14:36:03 +00:00
|
|
|
it 'POST /api/v2/maps/:id/stars' do
|
|
|
|
post "/api/v2/maps/#{map.id}/stars", params: { access_token: token }
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(response).to match_json_schema(:map)
|
|
|
|
expect(user.stars.count).to eq 1
|
|
|
|
expect(map.stars.count).to eq 1
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'DELETE /api/v2/maps/:id/stars' do
|
|
|
|
create(:star, map: map, user: user)
|
|
|
|
delete "/api/v2/maps/#{map.id}/stars", params: { access_token: token }
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:no_content)
|
|
|
|
expect(user.stars.count).to eq 0
|
|
|
|
expect(map.stars.count).to eq 0
|
|
|
|
end
|
|
|
|
|
2016-09-21 17:22:40 +00:00
|
|
|
context 'RAML example' do
|
|
|
|
let(:resource) { get_json_example(:map) }
|
|
|
|
let(:collection) { get_json_example(:maps) }
|
2016-10-29 14:58:31 +00:00
|
|
|
let(:starred) { get_json_example(:map_starred) }
|
2016-09-21 17:22:40 +00:00
|
|
|
|
|
|
|
it 'resource matches schema' do
|
|
|
|
expect(resource).to match_json_schema(:map)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'collection matches schema' do
|
|
|
|
expect(collection).to match_json_schema(:maps)
|
|
|
|
end
|
2016-10-29 14:58:31 +00:00
|
|
|
|
|
|
|
it 'starred resource matches schema' do
|
|
|
|
expect(starred).to match_json_schema(:map)
|
|
|
|
end
|
2016-09-21 17:22:40 +00:00
|
|
|
end
|
|
|
|
end
|