Skip to content

Commit

Permalink
tests done
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankFang committed May 15, 2020
1 parent 4fb1700 commit ae6bf51
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 21 deletions.
24 changes: 3 additions & 21 deletions README.md
@@ -1,24 +1,6 @@
# README

This README would normally document whatever steps are necessary to get the
application up and running.
```bash
docker run -v demo_data:/var/lib/postgresql/data -p 5005:5432 -e POSTGRES_USER=demo -d postgres
```

Things you may want to cover:

* Ruby version

* System dependencies

* Configuration

* Database creation

* Database initialization

* How to run the test suite

* Services (job queues, cache servers, search engines, etc.)

* Deployment instructions

* ...
4 changes: 4 additions & 0 deletions app/controllers/application_controller.rb
@@ -1,2 +1,6 @@
class ApplicationController < ActionController::API
rescue_from ActiveRecord::RecordNotFound, with: :render_404
def render_404
head 404
end
end
25 changes: 25 additions & 0 deletions app/controllers/users_controller.rb
@@ -1,4 +1,14 @@
class UsersController < ApplicationController

def index
render json: {resources: User.all}
end

def show
user = User.find params[:id]
render json: {resource: user}
end

def create
user = User.create get_params
if user.valid?
Expand All @@ -8,7 +18,22 @@ def create
end
end

def update
user = User.find params[:id]
if user.update(nickname: params[:nickname])
render json: {resource: user}
else
render json: {errors: user.errors}
end
end

def destroy
User.destroy params[:id]
head 200
end

def get_params
params.permit(:username, :password, :password_confirmation)
end

end
2 changes: 2 additions & 0 deletions app/models/user.rb
@@ -1,5 +1,7 @@
class User < ApplicationRecord
validates_presence_of :username
validates_presence_of :password_confirmation, on: :create
validates :nickname, length: { maximum: 100 }

has_secure_password
end
56 changes: 56 additions & 0 deletions spec/requests/users_request_spec.rb
Expand Up @@ -11,4 +11,60 @@
}.to change { User.count }.by(+1)
end
end

describe "Index" do
it 'should get all users' do
u = User.create username: 'frank', password: '123456', password_confirmation: '123456'
get :index
body = JSON.parse response.body
expect(body['resources'].length).to eq 1
end
end

describe "Show" do
it 'should show a user' do
u = User.create username: 'frank', password: '123456', password_confirmation: '123456'
get :show, params: {id: u.id}
body = JSON.parse response.body
expect(body['resource']['id']).to eq u.id
end

it 'should not show a user' do
get :show, params: {id: 444}
expect(response.status).to eq 404
end
end

describe "Destroy" do
it 'should destroy a user' do
u = User.create username: 'frank', password: '123456', password_confirmation: '123456'
expect {
delete :destroy, params: {id: u.id}
expect(response.status).to eq 200
}.to change { User.count }.by(-1)
end

it 'should not destroy a user' do
expect {
delete :destroy, params: {id: 444}
expect(response.status).to eq 404
}.to change { User.count }.by(-1)
end
end

describe 'Update' do
it 'should update a user' do
u = User.create username: 'frank', password: '123456', password_confirmation: '123456'
patch :update, params: {id: u.id, nickname:'Jack'}
body = JSON.parse response.body
expect(body['resource']['nickname']).to eq 'Jack'
end
it 'should get errors' do
u = User.create username: 'frank', password: '123456', password_confirmation: '123456'
patch :update, params: {id: u.id, nickname:'JackJackJackJackJackJackJackJackJackJackJackJackJackJackJackJackJackJackJackJackJackJackJackJackJackJackJackJackJackJackJack'}
body = JSON.parse response.body
p body
expect(body['errors']['nickname']).not_to be_nil
end
end
end

0 comments on commit ae6bf51

Please sign in to comment.