Skip to content

Commit

Permalink
Merge pull request #31 from tan12082001/controllers_tests
Browse files Browse the repository at this point in the history
Create Controllers tests
  • Loading branch information
hetrox8 authored Dec 12, 2023
2 parents 7a56d9f + 8713a3b commit 1596f8b
Show file tree
Hide file tree
Showing 7 changed files with 329 additions and 9 deletions.
30 changes: 22 additions & 8 deletions spec/examples.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
example_id | status | run_time |
------------------------------------------- | ------ | --------------- |
./spec/models/car_spec.rb[1:1:1] | passed | 0.00187 seconds |
./spec/models/car_spec.rb[1:1:2] | passed | 0.05522 seconds |
./spec/models/my_reservation_spec.rb[1:1:1] | passed | 0.03853 seconds |
./spec/models/my_reservation_spec.rb[1:1:2] | passed | 0.00198 seconds |
./spec/models/user_spec.rb[1:1:1] | passed | 0.00376 seconds |
./spec/models/user_spec.rb[1:1:2] | passed | 0.11558 seconds |
example_id | status | run_time |
---------------------------------------------------- | ------ | --------------- |
./spec/models/car_spec.rb[1:1:1] | passed | 0.00587 seconds |
./spec/models/car_spec.rb[1:1:2] | passed | 0.14159 seconds |
./spec/models/my_reservation_spec.rb[1:1:1] | passed | 0.00397 seconds |
./spec/models/my_reservation_spec.rb[1:1:2] | passed | 0.0029 seconds |
./spec/models/user_spec.rb[1:1:1] | passed | 0.00157 seconds |
./spec/models/user_spec.rb[1:1:2] | passed | 0.002 seconds |
./spec/requests/api/cars_spec.rb[1:1:1] | passed | 0.05168 seconds |
./spec/requests/api/cars_spec.rb[1:2:1:1] | passed | 2.02 seconds |
./spec/requests/api/cars_spec.rb[1:2:2:1] | passed | 1.78 seconds |
./spec/requests/api/cars_spec.rb[1:3:1:1] | passed | 0.02911 seconds |
./spec/requests/api/cars_spec.rb[1:3:2:1] | passed | 0.02891 seconds |
./spec/requests/api/cars_spec.rb[1:4:1:1] | passed | 0.02337 seconds |
./spec/requests/api/cars_spec.rb[1:4:2:1] | passed | 0.55206 seconds |
./spec/requests/api/my_reservations_spec.rb[1:1:1] | passed | 0.02825 seconds |
./spec/requests/api/my_reservations_spec.rb[1:2:1:1] | passed | 0.0311 seconds |
./spec/requests/api/my_reservations_spec.rb[1:2:2:1] | passed | 0.05047 seconds |
./spec/requests/api/registrations_spec.rb[1:1:1:1] | passed | 0.01067 seconds |
./spec/requests/api/registrations_spec.rb[1:1:2:1] | passed | 0.10001 seconds |
./spec/requests/api/sessions_spec.rb[1:1:1:1] | passed | 0.02346 seconds |
./spec/requests/api/sessions_spec.rb[1:1:2:1] | passed | 0.01105 seconds |
1 change: 0 additions & 1 deletion spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
rescue ActiveRecord::PendingMigrationError => e
abort e.to_s.strip
end

RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_paths = [
Expand Down
116 changes: 116 additions & 0 deletions spec/requests/api/cars_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
require 'swagger_helper'
require 'rails_helper'

RSpec.describe 'Api::CarsControllers', type: :request do
before do
post '/api/users', params: {
user: {
username: 'user',
email: 'user@example.com',
password: 'password'
}
}
post '/api/users/sign_in', params: {
user: {
username: 'user',
password: 'password'
}
}
session = JSON.parse(response.body).deep_symbolize_keys
@token = session[:token]
end

describe 'GET /index' do
it 'returns an array of cars' do
get '/api/all_cars'
json = JSON.parse(response.body)
expect(response.status).to eq(200)
expect(json).to be_a(Array)
end
end

describe 'GET /show' do
context 'request for an existing car' do
it 'returns a single car' do
car = Car.create(name: 'car', description: 'description of car', pricePerHr: 20, image: 'photo.jpg',
seating_capacity: 3, rental_duration: 3)
get "/api/cars/#{car.id}"

json = JSON.parse(response.body)
expect(response.status).to eq(200)
expect(json['name']).to eq('car')
expect(json['description']).to eq('description of car')
end
end

context 'request for a non-existent car' do
it 'returns an error' do
id = 1138
get "/api/cars/#{id}"

expect(response.status).to eq(404)
end
end
end

describe 'POST /create' do
context 'creating a car with the correct parameters' do
it 'creates a car' do
post '/api/car/new_car', params: {
car: {
name: 'test_car',
description: 'description of test_car',
pricePerHr: 20,
image: 'photo.jpg',
seating_capacity: 3,
rental_duration: 3
}
}

json = JSON.parse(response.body)
expect(response.status).to eq(201)
expect(json['name']).to eq('test_car')
expect(json['description']).to eq('description of test_car')
end
end

context 'creating a car with the wrong parameters' do
it 'returns an error message' do
post '/api/car/new_car', params: {
car: {
name: '',
description: ''
}
}

JSON.parse(response.body)
expect(response.status).to eq(422)
end
end
end

describe 'DELETE /destroy' do
context 'a delete request for an existing car' do
it 'deletes a car' do
car = Car.create(name: 'car2', description: 'description for car2', pricePerHr: 20, image: 'photo.jpg',
seating_capacity: 3, rental_duration: 3)
delete "/api/cars/#{car.id}", headers: { Authorization: "Bearer #{@token}" }

json = JSON.parse(response.body)
expect(response.status).to eq(200)
expect(json['message']).to eq('Car marked as removed')
end
end

context 'a delete request for an non-existent car' do
it 'returns an error message' do
car = Car.create(name: 'car2', description: 'description for car2', pricePerHr: 20, image: 'photo.jpg',
seating_capacity: 3, rental_duration: 3)
Car.destroy(car.id)
delete "/api/cars/#{car.id}", headers: { Authorization: "Bearer #{@token}" }

expect(response.status).to eq(404)
end
end
end
end
66 changes: 66 additions & 0 deletions spec/requests/api/my_reservations_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
require 'swagger_helper'
require 'rails_helper'

RSpec.describe 'Api::MyReservationsControllers', type: :request do
before do
post '/api/users', params: {
user: {
username: 'user',
email: 'user@example.com',
password: 'password'
}
}
post '/api/users/sign_in', params: {
user: {
username: 'user',
password: 'password'
}
}
session = JSON.parse(response.body).deep_symbolize_keys
@token = session[:token]
end

describe 'GET /index' do
it 'returns an array of reservations made by the current user' do
get '/api/my_reservations'
JSON.parse(response.body)
expect(response.status).to eq(200)
end
end

describe 'POST /create' do
context 'creating a reservation with valid parameters' do
it 'creates a reservation' do
car = Car.create(name: 'car', description: 'description of car', pricePerHr: 20, image: 'photo.jpg',
seating_capacity: 3, rental_duration: 3)
post "/api/car/#{car.id}/new_reserve", params: {
my_reservation: {
date: '2024-10-10',
city: 'Lagos'
}
}

json = JSON.parse(response.body)
expect(response.status).to eq(201)
expect(json['city']).to eq('Lagos')
expect(json['date']).to eq('2024-10-10')
expect(json['car_id']).to eq(car.id)
end
end

context 'creating a reservation with invalid parameters' do
it 'returns an error' do
car = Car.create(name: 'car', description: 'description of car', pricePerHr: 20, image: 'photo.jpg',
seating_capacity: 3, rental_duration: 3)
post "/api/car/#{car.id}/new_reserve", params: {
my_reservation: {
date: '',
city: ''
}
}

expect(response.status).to eq(422)
end
end
end
end
38 changes: 38 additions & 0 deletions spec/requests/api/registrations_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'swagger_helper'
require 'rails_helper'

RSpec.describe 'Api::RegistrationsControllers', type: :request do
describe 'POST /create' do
context 'when user registration is successful' do
it 'creates a new user' do
post '/api/users/', params: {
user: {
username: 'user',
email: 'user@example.com',
password: 'password'
}
}

json = JSON.parse(response.body).deep_symbolize_keys
expect(response.status).to eq(200)
expect(json[:message]).to eq('Signup successful')
expect(json[:user][:username]).to eq('user')
end
end

context 'when user registration fails' do
it 'returns an error message' do
post '/api/users', params: {
user: {
username: ''
}
}
json = JSON.parse(response.body).deep_symbolize_keys
expect(response.status).to eq(422)
expect(json[:errors]).to include("Username can't be blank")
expect(json[:errors]).to include("Email can't be blank")
expect(json[:errors]).to include("Password can't be blank")
end
end
end
end
46 changes: 46 additions & 0 deletions spec/requests/api/sessions_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'swagger_helper'
require 'rails_helper'

RSpec.describe 'Api::SessionsControllers', type: :request do
before do
post '/api/users', params: {
user: {
username: 'user',
email: 'user@example.com',
password: 'password'
}
}
end

describe 'POST /create' do
context 'successful user login' do
it 'logs a user in' do
post '/api/users/sign_in', params: {
user: {
username: 'user',
password: 'password'
}
}

json = JSON.parse(response.body).deep_symbolize_keys
expect(response.status).to eq(200)
expect(json[:message]).to eq('Login successful')
end
end

context 'failed user login' do
it 'returns an error message' do
post '/api/users/sign_in', params: {
user: {
username: 'login_user',
password: 'password'
}
}

json = JSON.parse(response.body).deep_symbolize_keys
expect(response.status).to eq(401)
expect(json[:message]).to eq('Invalid username or password')
end
end
end
end
41 changes: 41 additions & 0 deletions spec/swagger_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'rails_helper'

RSpec.configure do |config|
# Specify a root folder where Swagger JSON files are generated
# NOTE: If you're using the rswag-api to serve API descriptions, you'll need
# to ensure that it's configured to serve Swagger from the same folder
config.openapi_root = Rails.root.join('swagger').to_s

# Define one or more Swagger documents and provide global metadata for each one
# When you run the 'rswag:specs:swaggerize' rake task, the complete Swagger will
# be generated at the provided relative path under openapi_root
# By default, the operations defined in spec files are added to the first
# document below. You can override this behavior by adding a openapi_spec tag to the
# the root example_group in your specs, e.g. describe '...', openapi_spec: 'v2/swagger.json'
config.openapi_specs = {
'v1/swagger.yaml' => {
openapi: '3.0.1',
info: {
title: 'API V1',
version: 'v1'
},
paths: {},
servers: [
{
url: 'https://{defaultHost}',
variables: {
defaultHost: {
default: 'www.example.com'
}
}
}
]
}
}

# Specify the format of the output Swagger file when running 'rswag:specs:swaggerize'.
# The openapi_specs configuration option has the filename including format in
# the key, this may want to be changed to avoid putting yaml in json files.
# Defaults to json. Accepts ':json' and ':yaml'.
config.openapi_format = :yaml
end

0 comments on commit 1596f8b

Please sign in to comment.