-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* first groups api test * code style fixes * started writing controller and fixed test * finished controller and fixed tests * postman collection for backend * code style fixes * code style fixes 2
- Loading branch information
Showing
10 changed files
with
292 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
class ApplicationController < ActionController::API | ||
include Response | ||
include ExceptionHandler | ||
end |
13 changes: 13 additions & 0 deletions
13
todo_backend/app/controllers/concerns/exception_handler.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module ExceptionHandler | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
rescue_from ActiveRecord::RecordNotFound do |e| | ||
json_response({ message: e.message }, :not_found) | ||
end | ||
|
||
rescue_from ActiveRecord::RecordInvalid do |e| | ||
json_response({ message: e.message }, :unprocessable_entity) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module Response | ||
def json_response(object, status = :ok) | ||
render json: object, status: status | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
class GroupController < ApplicationController | ||
# skip_before_action :verify_authenticity_token | ||
before_action :set_group, only: [ :update, :destroy ] | ||
|
||
# GET /group | ||
def index | ||
@groups = Group.all | ||
json_response(@groups) | ||
end | ||
|
||
# POST /group | ||
def create | ||
@group = Group.create!(group_params) | ||
json_response(@group, :created) | ||
end | ||
|
||
# PUT /group/:id | ||
def update | ||
@group.update(group_params) | ||
head :no_content | ||
end | ||
|
||
# DELETE /group/:id | ||
def destroy | ||
@group.destroy | ||
head :no_content | ||
end | ||
|
||
private | ||
|
||
def group_params | ||
params.permit(:name) | ||
end | ||
|
||
def set_group | ||
@group = Group.find(params[:id]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,5 @@ | |
|
||
# Defines the root path route ("/") | ||
# root "posts#index" | ||
resources :group | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
FactoryBot.define do | ||
factory :group do | ||
name { Faker::Lorem.unique.word } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe "Groups", type: :request do | ||
# describe "GET /index" do | ||
# pending "add some examples (or delete) #{__FILE__}" | ||
# end | ||
let!(:groups) { create_list(:group, 10) } | ||
let(:group_id) { groups.first.id.to_s } | ||
|
||
# Get list of groups | ||
describe "GET /group" do | ||
before { get '/group' } | ||
|
||
it 'returns groups' do | ||
expect(json).not_to be_empty | ||
expect(json.size).to eq(10) | ||
end | ||
|
||
it 'returns status code 200' do | ||
expect(response).to have_http_status(200) | ||
end | ||
end | ||
|
||
# Create new group | ||
describe "POST /group" do | ||
let(:valid_attributes) { { name: 'Group' } } | ||
|
||
context "request is valid" do | ||
before { post '/group', params: valid_attributes } | ||
|
||
it "creates a group" do | ||
expect(json['name']).to eq('Group') | ||
end | ||
|
||
it "returns status code 201" do | ||
expect(response).to have_http_status(201) | ||
end | ||
end | ||
|
||
context "request is invalid" do | ||
before { post '/group', params: {} } | ||
|
||
it "returns status code 422" do | ||
expect(response).to have_http_status(422) | ||
end | ||
|
||
it "returns validation presence failure" do | ||
expect(response.body) | ||
.to match("{\"message\":\"Validation failed: Name can't be blank\"}") | ||
end | ||
end | ||
# TODO other validations | ||
end | ||
|
||
# Update existing group | ||
describe "PUT /group/:id" do | ||
let(:valid_attributes) { { name: 'Tasks' } } | ||
|
||
context "record exists" do | ||
before { put '/group/' + group_id, params: valid_attributes } | ||
|
||
it "updates record" do | ||
expect(response.body).to be_empty | ||
end | ||
|
||
it "returns status code 204" do | ||
expect(response).to have_http_status(204) | ||
end | ||
end | ||
end | ||
|
||
# Delete group | ||
describe "DELETE /group/:id" do | ||
before { delete '/group/' + group_id } | ||
|
||
it "returns status code 204" do | ||
expect(response).to have_http_status(204) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module RequestSpecHelper | ||
def json | ||
JSON.parse(response.body) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
{ | ||
"info": { | ||
"_postman_id": "e45012ad-df75-41a5-83cd-f8ad1ddb265e", | ||
"name": "todo_vue_rails", | ||
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", | ||
"_exporter_id": "29255098" | ||
}, | ||
"item": [ | ||
{ | ||
"name": "group", | ||
"item": [ | ||
{ | ||
"name": "List groups", | ||
"request": { | ||
"method": "GET", | ||
"header": [], | ||
"url": { | ||
"raw": "{{ADDR}}/group", | ||
"host": [ | ||
"{{ADDR}}" | ||
], | ||
"path": [ | ||
"group" | ||
] | ||
} | ||
}, | ||
"response": [] | ||
}, | ||
{ | ||
"name": "Create group", | ||
"request": { | ||
"method": "POST", | ||
"header": [], | ||
"url": { | ||
"raw": "{{ADDR}}/group?name", | ||
"host": [ | ||
"{{ADDR}}" | ||
], | ||
"path": [ | ||
"group" | ||
], | ||
"query": [ | ||
{ | ||
"key": "name", | ||
"value": null | ||
} | ||
] | ||
} | ||
}, | ||
"response": [] | ||
}, | ||
{ | ||
"name": "Update group", | ||
"request": { | ||
"method": "PUT", | ||
"header": [], | ||
"url": { | ||
"raw": "{{ADDR}}/group/:id?name", | ||
"host": [ | ||
"{{ADDR}}" | ||
], | ||
"path": [ | ||
"group", | ||
":id" | ||
], | ||
"query": [ | ||
{ | ||
"key": "name", | ||
"value": null | ||
} | ||
], | ||
"variable": [ | ||
{ | ||
"key": "id", | ||
"value": "" | ||
} | ||
] | ||
} | ||
}, | ||
"response": [] | ||
}, | ||
{ | ||
"name": "Delete group", | ||
"request": { | ||
"method": "DELETE", | ||
"header": [], | ||
"url": { | ||
"raw": "{{ADDR}}/group/:id", | ||
"host": [ | ||
"{{ADDR}}" | ||
], | ||
"path": [ | ||
"group", | ||
":id" | ||
], | ||
"variable": [ | ||
{ | ||
"key": "id", | ||
"value": "" | ||
} | ||
] | ||
} | ||
}, | ||
"response": [] | ||
} | ||
] | ||
} | ||
], | ||
"event": [ | ||
{ | ||
"listen": "prerequest", | ||
"script": { | ||
"type": "text/javascript", | ||
"exec": [ | ||
"" | ||
] | ||
} | ||
}, | ||
{ | ||
"listen": "test", | ||
"script": { | ||
"type": "text/javascript", | ||
"exec": [ | ||
"" | ||
] | ||
} | ||
} | ||
], | ||
"variable": [ | ||
{ | ||
"key": "ADDR", | ||
"value": "http://localhost:3000", | ||
"type": "string" | ||
} | ||
] | ||
} |