2024-08-21 13:20:04 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
|
|
RSpec.describe 'Api::V1::Points', type: :request do
|
|
|
|
|
let!(:user) { create(:user) }
|
2025-01-20 11:59:13 -05:00
|
|
|
let!(:points) do
|
|
|
|
|
(1..15).map do |i|
|
|
|
|
|
create(:point, user:, timestamp: 1.day.ago + i.minutes)
|
|
|
|
|
end
|
|
|
|
|
end
|
2024-08-21 13:20:04 -04:00
|
|
|
|
|
|
|
|
describe 'GET /index' do
|
2024-09-23 18:10:39 -04:00
|
|
|
context 'when regular version of points is requested' do
|
|
|
|
|
it 'renders a successful response' do
|
|
|
|
|
get api_v1_points_url(api_key: user.api_key)
|
2024-08-21 13:20:04 -04:00
|
|
|
|
2024-09-23 18:10:39 -04:00
|
|
|
expect(response).to be_successful
|
|
|
|
|
end
|
2024-08-21 13:20:04 -04:00
|
|
|
|
2024-09-23 18:10:39 -04:00
|
|
|
it 'returns a list of points' do
|
|
|
|
|
get api_v1_points_url(api_key: user.api_key)
|
2024-08-21 13:20:04 -04:00
|
|
|
|
2024-09-23 18:10:39 -04:00
|
|
|
expect(response).to have_http_status(:ok)
|
2024-08-21 13:20:04 -04:00
|
|
|
|
2024-09-23 18:10:39 -04:00
|
|
|
json_response = JSON.parse(response.body)
|
2024-08-21 13:20:04 -04:00
|
|
|
|
2025-01-20 11:59:13 -05:00
|
|
|
expect(json_response.size).to eq(15)
|
2024-09-23 18:10:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'returns a list of points with pagination' do
|
|
|
|
|
get api_v1_points_url(api_key: user.api_key, page: 2, per_page: 10)
|
|
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
|
|
|
|
|
|
|
|
json_response = JSON.parse(response.body)
|
2024-08-21 13:20:04 -04:00
|
|
|
|
2025-01-20 11:59:13 -05:00
|
|
|
expect(json_response.size).to eq(5)
|
2024-09-23 18:10:39 -04:00
|
|
|
end
|
2024-08-21 13:20:04 -04:00
|
|
|
|
2024-09-23 18:10:39 -04:00
|
|
|
it 'returns a list of points with pagination headers' do
|
|
|
|
|
get api_v1_points_url(api_key: user.api_key, page: 2, per_page: 10)
|
2024-08-21 13:20:04 -04:00
|
|
|
|
2024-09-23 18:10:39 -04:00
|
|
|
expect(response).to have_http_status(:ok)
|
2024-08-21 13:20:04 -04:00
|
|
|
|
2024-09-23 18:10:39 -04:00
|
|
|
expect(response.headers['X-Current-Page']).to eq('2')
|
2025-01-20 11:59:13 -05:00
|
|
|
expect(response.headers['X-Total-Pages']).to eq('2')
|
2024-09-23 18:10:39 -04:00
|
|
|
end
|
2024-08-21 13:20:04 -04:00
|
|
|
end
|
2024-09-14 16:52:25 -04:00
|
|
|
|
2024-09-23 18:10:39 -04:00
|
|
|
context 'when slim version of points is requested' do
|
|
|
|
|
it 'renders a successful response' do
|
2024-09-30 17:38:32 -04:00
|
|
|
get api_v1_points_url(api_key: user.api_key, slim: 'true')
|
2024-09-23 18:10:39 -04:00
|
|
|
|
|
|
|
|
expect(response).to be_successful
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'returns a list of points' do
|
2024-09-30 17:38:32 -04:00
|
|
|
get api_v1_points_url(api_key: user.api_key, slim: 'true')
|
2024-09-23 18:10:39 -04:00
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
|
|
|
|
|
|
|
|
json_response = JSON.parse(response.body)
|
|
|
|
|
|
2025-01-20 11:59:13 -05:00
|
|
|
expect(json_response.size).to eq(15)
|
2024-09-23 18:10:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'returns a list of points with pagination' do
|
2024-09-30 17:38:32 -04:00
|
|
|
get api_v1_points_url(api_key: user.api_key, slim: 'true', page: 2, per_page: 10)
|
2024-09-23 18:10:39 -04:00
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
|
|
|
|
|
|
|
|
json_response = JSON.parse(response.body)
|
|
|
|
|
|
2025-01-20 11:59:13 -05:00
|
|
|
expect(json_response.size).to eq(5)
|
2024-09-23 18:10:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'returns a list of points with pagination headers' do
|
2024-09-30 17:38:32 -04:00
|
|
|
get api_v1_points_url(api_key: user.api_key, slim: 'true', page: 2, per_page: 10)
|
2024-09-23 18:10:39 -04:00
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
|
|
|
|
|
|
|
|
expect(response.headers['X-Current-Page']).to eq('2')
|
2025-01-20 11:59:13 -05:00
|
|
|
expect(response.headers['X-Total-Pages']).to eq('2')
|
2024-09-23 18:10:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'returns a list of points with slim attributes' do
|
2024-09-30 17:38:32 -04:00
|
|
|
get api_v1_points_url(api_key: user.api_key, slim: 'true')
|
2024-09-23 18:10:39 -04:00
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:ok)
|
2024-09-14 16:52:25 -04:00
|
|
|
|
2024-09-23 18:10:39 -04:00
|
|
|
json_response = JSON.parse(response.body)
|
2024-09-14 16:52:25 -04:00
|
|
|
|
2024-09-23 18:10:39 -04:00
|
|
|
json_response.each do |point|
|
2024-10-02 15:29:56 -04:00
|
|
|
expect(point.keys).to eq(%w[id latitude longitude timestamp])
|
2024-09-23 18:10:39 -04:00
|
|
|
end
|
|
|
|
|
end
|
2024-09-14 16:52:25 -04:00
|
|
|
end
|
2024-10-02 15:29:56 -04:00
|
|
|
|
|
|
|
|
context 'when order param is provided' do
|
|
|
|
|
it 'returns points in ascending order' do
|
|
|
|
|
get api_v1_points_url(api_key: user.api_key, order: 'asc')
|
|
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
|
|
|
|
|
|
|
|
json_response = JSON.parse(response.body)
|
|
|
|
|
|
|
|
|
|
expect(json_response.first['timestamp']).to be < json_response.last['timestamp']
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'returns points in descending order' do
|
|
|
|
|
get api_v1_points_url(api_key: user.api_key, order: 'desc')
|
|
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
|
|
|
|
|
|
|
|
json_response = JSON.parse(response.body)
|
|
|
|
|
|
|
|
|
|
expect(json_response.first['timestamp']).to be > json_response.last['timestamp']
|
|
|
|
|
end
|
|
|
|
|
end
|
2024-08-21 13:20:04 -04:00
|
|
|
end
|
2025-02-19 15:23:11 -05:00
|
|
|
|
|
|
|
|
describe 'POST /create' do
|
|
|
|
|
it 'returns a successful response' do
|
|
|
|
|
post "/api/v1/points?api_key=#{user.api_key}", params: { point: { latitude: 1.0, longitude: 1.0 } }
|
|
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when user is inactive' do
|
|
|
|
|
before do
|
|
|
|
|
user.update(status: :inactive)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'returns an unauthorized response' do
|
|
|
|
|
post "/api/v1/points?api_key=#{user.api_key}", params: { point: { latitude: 1.0, longitude: 1.0 } }
|
|
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe 'PUT /update' do
|
|
|
|
|
it 'returns a successful response' do
|
|
|
|
|
put "/api/v1/points/#{points.first.id}?api_key=#{user.api_key}",
|
|
|
|
|
params: { point: { latitude: 1.0, longitude: 1.1 } }
|
|
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when user is inactive' do
|
|
|
|
|
before do
|
|
|
|
|
user.update(status: :inactive)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'returns an unauthorized response' do
|
|
|
|
|
put "/api/v1/points/#{points.first.id}?api_key=#{user.api_key}",
|
|
|
|
|
params: { point: { latitude: 1.0, longitude: 1.1 } }
|
|
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe 'DELETE /destroy' do
|
|
|
|
|
it 'returns a successful response' do
|
|
|
|
|
delete "/api/v1/points/#{points.first.id}?api_key=#{user.api_key}"
|
|
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when user is inactive' do
|
|
|
|
|
before do
|
|
|
|
|
user.update(status: :inactive)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'returns an unauthorized response' do
|
|
|
|
|
delete "/api/v1/points/#{points.first.id}?api_key=#{user.api_key}"
|
|
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2024-08-21 13:20:04 -04:00
|
|
|
end
|