From 2a42d3069ca6ac21c16d3e84b753486ad70822dd Mon Sep 17 00:00:00 2001 From: riggraz Date: Wed, 4 Sep 2019 17:37:08 +0200 Subject: [PATCH] Add tests of controllers --- app/controllers/posts_controller.rb | 12 +-- config/routes.rb | 6 +- spec/controllers/posts_controller_spec.rb | 5 - .../requests/post_statuses_controller_test.rb | 25 +++++ spec/requests/posts_controller_test.rb | 100 ++++++++++++++++++ ...routing_spec.rb => boards_routing_spec.rb} | 4 +- spec/routing/post_routing_spec.rb | 17 +++ 7 files changed, 152 insertions(+), 17 deletions(-) delete mode 100644 spec/controllers/posts_controller_spec.rb create mode 100644 spec/requests/post_statuses_controller_test.rb create mode 100644 spec/requests/posts_controller_test.rb rename spec/routing/{board_routing_spec.rb => boards_routing_spec.rb} (76%) create mode 100644 spec/routing/post_routing_spec.rb diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index fc73909d..d3432bb1 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -1,10 +1,7 @@ class PostsController < ApplicationController before_action :authenticate_user!, only: [:create] - def index_by_board_id - board_id = params[:board_id] || 1 - - + def index posts = Post .left_outer_joins(:post_status) .select('posts.title, posts.description, post_statuses.name as post_status_name, post_statuses.color as post_status_color') @@ -28,7 +25,11 @@ class PostsController < ApplicationController private def filter_params - params.permit(:board_id, :post_status_id) + defaults = { board_id: Board.first.id } + + params + .permit(:board_id, :post_status_id) + .with_defaults(defaults) end def post_params @@ -37,5 +38,4 @@ class PostsController < ApplicationController .permit(:title, :description, :board_id) .merge(user_id: current_user.id) end - end diff --git a/config/routes.rb b/config/routes.rb index c6b9b54a..b584e64a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -13,8 +13,6 @@ Rails.application.routes.draw do devise_for :users resources :boards, only: [:show] - - post '/posts', to: 'posts#create' - get '/posts', to: 'posts#index_by_board_id' - get '/post_statuses', to: 'post_statuses#index' + resources :posts, only: [:index, :create] + resources :post_statuses, only: [:index] end diff --git a/spec/controllers/posts_controller_spec.rb b/spec/controllers/posts_controller_spec.rb deleted file mode 100644 index d2e1d4b4..00000000 --- a/spec/controllers/posts_controller_spec.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'rails_helper' - -RSpec.describe PostsController, type: :controller do - -end diff --git a/spec/requests/post_statuses_controller_test.rb b/spec/requests/post_statuses_controller_test.rb new file mode 100644 index 00000000..965f19ca --- /dev/null +++ b/spec/requests/post_statuses_controller_test.rb @@ -0,0 +1,25 @@ +require 'rails_helper' + +RSpec.describe 'post statuses controller' do + let(:post_status1) { FactoryBot.create(:post_status) } + let(:post_status2) { FactoryBot.create(:post_status) } + + def create_post_statuses + post_status1 + post_status2 + end + + describe 'index' do + before(:each) { create_post_statuses } + + it 'returns all post statuses as JSON' do + get post_statuses_path + + json = JSON.parse(response.body) + + expect(response).to have_http_status(:success) + expect(json.length).to eq(2) + expect(json[0]['order']).to be <= json[1]['order'] + end + end +end \ No newline at end of file diff --git a/spec/requests/posts_controller_test.rb b/spec/requests/posts_controller_test.rb new file mode 100644 index 00000000..0bd74bec --- /dev/null +++ b/spec/requests/posts_controller_test.rb @@ -0,0 +1,100 @@ +require 'rails_helper' + +RSpec.describe 'posts controller', type: :request do + let (:user) { FactoryBot.create(:user) } + + let(:board1) { FactoryBot.create(:board) } + let(:board2) { FactoryBot.create(:board) } + + let(:post1) { FactoryBot.create(:post, board: board1) } + let(:post2) { FactoryBot.create(:post, board: board1) } + let(:post3) { FactoryBot.create(:post, board: board2) } + + def create_posts + post1 + post2 + post3 + end + + describe 'index' do + before(:each) { create_posts } + + it 'returns posts of first board (if no board id provided) as JSON' do + get posts_path + json1 = JSON.parse(response.body) + + get posts_path(board_id: Board.first.id) + json2 = JSON.parse(response.body) + + expect(json1).to eq(json2) + end + + it 'returns posts filtered by board id as JSON' do + get posts_path(board_id: post1.board_id) + + json = JSON.parse(response.body) + + expect(response).to have_http_status(:success) + expect(json.length).to eq(2) + end + + it 'returns posts filtered by post status id as JSON' do + get posts_path(board_id: post1.board_id, post_status_id: post1.post_status_id) + + json = JSON.parse(response.body) + + expect(response).to have_http_status(:success) + expect(json.length).to eq(1) + end + end + + describe 'create' do + before(:each) do + user.confirm + sign_in user + end + + it 'creates a new post' do + post_count = Post.count + + new_post = FactoryBot.build(:post, board: board1) + + post( + posts_path, + params: { + post: { + title: new_post.title, + description: new_post.description, + board_id: new_post.board_id + } + } + ) + + expect(response).to have_http_status(:success) + expect(Post.count).to eq(post_count + 1) + end + + it 'redirects if not logged in' do + sign_out user + + post_count = Post.count + + new_post = FactoryBot.build(:post, board: board1) + + post( + posts_path, + params: { + post: { + title: new_post.title, + description: new_post.description, + board_id: new_post.board_id + } + } + ) + + expect(response).to have_http_status(:redirect) + expect(response).to redirect_to(new_user_session_path) + expect(Post.count).to eq(post_count) + end + end +end \ No newline at end of file diff --git a/spec/routing/board_routing_spec.rb b/spec/routing/boards_routing_spec.rb similarity index 76% rename from spec/routing/board_routing_spec.rb rename to spec/routing/boards_routing_spec.rb index d243f6aa..f5a92b3b 100644 --- a/spec/routing/board_routing_spec.rb +++ b/spec/routing/boards_routing_spec.rb @@ -1,7 +1,7 @@ require 'rails_helper' -RSpec.describe 'board routing', :aggregate_failures, type: :routing do - it 'only routes the page to show a board' do +RSpec.describe 'boards routing', :aggregate_failures, type: :routing do + it 'routes boards' do expect(get: '/boards/1').to route_to( controller: 'boards', action: 'show', id: '1' ) diff --git a/spec/routing/post_routing_spec.rb b/spec/routing/post_routing_spec.rb new file mode 100644 index 00000000..13da956b --- /dev/null +++ b/spec/routing/post_routing_spec.rb @@ -0,0 +1,17 @@ +require 'rails_helper' + +RSpec.describe 'posts routing', :aggregate_failures, type: :routing do + it 'routes posts' do + expect(get: '/posts').to route_to( + controller: 'posts', action: 'index' + ) + expect(post: '/posts').to route_to( + controller: 'posts', action: 'create' + ) + + expect(get: '/posts/1').not_to be_routable + expect(get: '/posts/1/edit').not_to be_routable + expect(patch: '/posts/1').not_to be_routable + expect(delete: '/posts/1').not_to be_routable + end +end \ No newline at end of file