Skip to content

Commit

Permalink
Merge branch 'master' into feature/home-page
Browse files Browse the repository at this point in the history
  • Loading branch information
renatamarques97 authored Jan 6, 2020
2 parents d20c28d + 57bac04 commit f8bf5b1
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 97 deletions.
5 changes: 3 additions & 2 deletions app/controllers/entries_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
class EntriesController < ApplicationController
before_action :set_entry, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, only: [:index, :new, :create]

# GET /entries
# GET /entries.json
def index
@entries = Entry.all
@entries = current_user.entries.all
end

# GET /entries/1
Expand All @@ -24,7 +25,7 @@ def edit
# POST /entries
# POST /entries.json
def create
@entry = Entry.new(entry_params)
@entry = current_user.entries.new(entry_params)

respond_to do |format|
if @entry.save
Expand Down
136 changes: 45 additions & 91 deletions spec/controllers/entries_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,7 @@
require 'rails_helper'

# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to specify the controller code that
# was generated by Rails when you ran the scaffold generator.
#
# It assumes that the implementation code is generated by the rails scaffold
# generator. If you are using any extension libraries to generate different
# controller code, this generated spec may or may not pass.
#
# It only uses APIs available in rails and/or rspec-rails. There are a number
# of tools you can use to make these specs even more expressive, but we're
# sticking to rails and rspec-rails APIs to keep things simple and stable.
#
# Compared to earlier versions of this generator, there is very limited use of
# stubs and message expectations in this spec. Stubs are only used when there
# is no simpler way to get a handle on the object needed for the example.
# Message expectations are only used when there is no simpler way to specify
# that an instance is receiving a specific message.
#
# Also compared to earlier versions of this generator, there are no longer any
# expectations of assigns and templates rendered. These features have been
# removed from Rails core in Rails 5, but can be added back in via the
# `rails-controller-testing` gem.

RSpec.describe EntriesController, type: :controller do

# This should return the minimal set of attributes required to create a valid
# Entry. As you add validations to Entry, be sure to
# adjust the attributes here as well.
let(:user) { create(:user) }
let(:valid_attributes) {
attributes_for(:entry, user_id: user.id)
Expand All @@ -37,89 +11,88 @@
attributes_for(:entry, user_id: nil)
}

# This should return the minimal set of values that should be in the session
# in order to pass any filters (e.g. authentication) defined in
# EntriesController. Be sure to keep this updated too.
let(:valid_session) { {} }

describe "GET #index" do
before do
create(:entry, valid_attributes)
context "when user is not logged in" do
it "redirects to login page" do
get :index

expect(response).to have_http_status(302)
end
end

it "returns a success response" do
get :index, params: {}, session: valid_session
expect(response).to be_successful
context "when user is logged in" do
it "returns a successful response" do
sign_in user
get :index

expect(response).to be_successful
end
end
end

describe "GET #show" do
let(:entry) { create(:entry, valid_attributes) }

it "returns a success response" do
get :show, params: {id: entry.to_param}, session: valid_session
get :show, params: { id: entry.to_param }

expect(response).to be_successful
end
end

describe "GET #new" do
it "returns a success response" do
get :new, params: {}, session: valid_session
expect(response).to be_successful
context "when user is not logged in" do
it "redirects to login page" do
get :new

expect(response).to have_http_status(302)
end
end

context "when user is logged in" do
it "returns a successful response" do
sign_in user
get :new

expect(response).to be_successful
end
end
end

describe "GET #edit" do
let(:entry) { create(:entry, valid_attributes) }

it "returns a success response" do
get :edit, params: { id: entry.to_param }, session: valid_session
get :edit, params: { id: entry.to_param }
expect(response).to be_successful
end
end

describe "POST #create" do
context "with valid params" do
before { sign_in user }

it "creates a new Entry" do
expect {
post :create, params: { entry: valid_attributes }, session: valid_session
post :create, params: { entry: valid_attributes }
}.to change(Entry, :count).by(1)
end

it "redirects to the created entry" do
post :create, params: { entry: valid_attributes }, session: valid_session
post :create, params: { entry: valid_attributes }
expect(response).to redirect_to(Entry.last)
end

it "render success flash message" do
post :create, params: { entry: valid_attributes }, session: valid_session
post :create, params: { entry: valid_attributes }
expect(flash[:success]).to_not be_nil
end

it "render success flash message text" do
post :create, params: { entry: valid_attributes }, session: valid_session
post :create, params: { entry: valid_attributes }
expect(flash[:success]).to match(/Entry was successfully created./)
end
end

context "with invalid params" do
it "returns a failure response (i.e. to display the 'new' template)" do
post :create, params: { entry: invalid_attributes }, session: valid_session
expect(response).not_to be_successful
end

it "render failure flash message" do
expect_any_instance_of(Entry).to receive(:save).and_return(false)
post :create, params: { entry: invalid_attributes }, session: valid_session
expect(flash[:error]).to_not be_nil
end

it "render failure flash message text" do
expect_any_instance_of(Entry).to receive(:save).and_return(false)
post :create, params: { entry: invalid_attributes }, session: valid_session
expect(flash[:error]).to match(/Sorry, an error has occured/)
end
end
end

describe "PUT #update" do
Expand All @@ -130,70 +103,51 @@
let(:entry) { create(:entry, user_id: user.id) }

it "updates the requested entry" do
put :update, params: { id: entry.to_param, entry: new_attributes }, session: valid_session
put :update, params: { id: entry.to_param, entry: new_attributes }
entry.reload
expect(entry.feeling).to eq("sad")
end

it "redirects to the entry" do
put :update, params: { id: entry.to_param, entry: valid_attributes }, session: valid_session
put :update, params: { id: entry.to_param, entry: valid_attributes }
expect(response).to redirect_to(entry)
end

it "render success flash message" do
put :update, params: { id: entry.to_param, entry: valid_attributes }, session: valid_session
put :update, params: { id: entry.to_param, entry: valid_attributes }
expect(flash[:success]).to_not be_nil
end

it "render success flash message text" do
put :update, params: { id: entry.to_param, entry: valid_attributes }, session: valid_session
put :update, params: { id: entry.to_param, entry: valid_attributes }
expect(flash[:success]).to match(/Entry was successfully updated./)
end
end

context "with invalid params" do
let(:entry) { create(:entry, valid_attributes) }

it "returns a success response (i.e. to display the 'edit' template)" do
put :update, params: {id: entry.to_param, entry: invalid_attributes}, session: valid_session
expect(response).not_to be_successful
end

it "render failure flash message" do
put :update, params: {id: entry.to_param, entry: invalid_attributes}, session: valid_session
expect(flash[:error]).to_not be_nil
end

it "render failure flash message text" do
put :update, params: {id: entry.to_param, entry: invalid_attributes}, session: valid_session
expect(flash[:error]).to match(/Sorry, an error has occured/)
end
end
end

describe "DELETE #destroy" do
it "destroys the requested entry" do
entry = Entry.create! valid_attributes
expect {
delete :destroy, params: {id: entry.to_param}, session: valid_session
delete :destroy, params: {id: entry.to_param}
}.to change(Entry, :count).by(-1)
end

it "redirects to the entries list" do
entry = Entry.create! valid_attributes
delete :destroy, params: {id: entry.to_param}, session: valid_session
delete :destroy, params: {id: entry.to_param}
expect(response).to redirect_to(entries_url)
end

it "render success flash message" do
entry = Entry.create! valid_attributes
post :destroy, params: {id: entry.to_param}, session: valid_session
post :destroy, params: {id: entry.to_param}
expect(flash[:success]).to_not be_nil
end

it "render success flash message text" do
entry = Entry.create! valid_attributes
post :destroy, params: {id: entry.to_param}, session: valid_session
post :destroy, params: {id: entry.to_param}
expect(flash[:success]).to match(/Entry was successfully destroyed./)
end
end
Expand Down
61 changes: 57 additions & 4 deletions spec/requests/entries_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,63 @@
require 'rails_helper'
require "rails_helper"

RSpec.describe "Entries", type: :request do
describe "GET /entries" do
it "returns the correct response code" do
get entries_path
expect(response).to have_http_status(200)
let(:user) { create(:user) }
let!(:user_entry) {
create(:entry, user: user, description: 'Current User Entry') }
let!(:other_entry) { create(:entry, description: 'Another Entry') }

context "when user is not logged in" do
it "redirects to login page" do
get entries_path

expect(response).to have_http_status(302)
end
end

context "when user is logged in" do
before do
sign_in user
get entries_path
end

it "returns a successful response" do
expect(response).to have_http_status(200)
end

context "shows only the entries wich belong to the user" do
it "shows the current user's entries" do
subject = response.body.include?(user_entry.description)

expect(subject).to be(true)
end

it "does not show the entries wich does not belong to the user" do
subject = response.body.include?(other_entry.description)

expect(subject).to be(false)
end
end
end
end

describe "POST /entries" do
let(:user) { create(:user) }
let(:valid_attributes) { attributes_for(:entry) }

before { sign_in user }

it "creates an entry" do
expect {
post "/entries", params: { entry: valid_attributes }
}.to change(Entry, :count).by(1)
end

it "creates an entry wich belongs to the current user" do
post "/entries", params: { entry: valid_attributes }
subject = Entry.last.user_id

expect(subject).to be(user.id)
end
end
end
4 changes: 4 additions & 0 deletions spec/support/devise.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
RSpec.configure do |config|
config.include Devise::Test::ControllerHelpers, type: :controller
config.include Devise::Test::IntegrationHelpers, type: :request
end

0 comments on commit f8bf5b1

Please sign in to comment.