Skip to content

Commit

Permalink
merge features/profiles-controller
Browse files Browse the repository at this point in the history
  • Loading branch information
npauzenga committed Jan 30, 2016
2 parents 8a7ae84 + 7415144 commit 8669034
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 70 deletions.
23 changes: 23 additions & 0 deletions app/controllers/profiles_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class ProfilesController < AuthenticationController
# GET /profiles/1
def show
result = ShowProfile.call(id: params[:id])

if result.success?
render json: result.user, status: :ok
else
render json: result.errors, status: :not_found
end
end

# GET /profiles
def index
result = IndexProfile.call

if result.success?
render json: result.profiles, status: :ok
else
render json: result.errors, status: :internal_server_error
end
end
end
19 changes: 4 additions & 15 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class UsersController < AuthenticationController
skip_before_action :authenticate, only: :create

# POST /users
# POST /user
def create
result = CreateUser.call(user_params: user_params)

Expand All @@ -13,20 +13,9 @@ def create
end
end

# GET /users/1
def show
result = ShowUser.call(id: params[:id])

if result.success?
render json: result.user, status: :ok
else
render json: result.errors, status: :not_found
end
end

# PATCH /users/1
# PATCH /user
def update
result = UpdateUser.call(user: current_user, user_params: user_params)
result = UpdateUser.call(user: current_user, params: user_params)

if result.success?
render json: result.user, status: :ok
Expand All @@ -35,7 +24,7 @@ def update
end
end

# DELETE /users/1
# DELETE /user
def destroy
result = DeleteUser.call(id: current_user.id)

Expand Down
9 changes: 9 additions & 0 deletions app/interactors/index_profile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class IndexProfile < StandardInteraction
def execute
context.profiles = User.all
end

def validate_output
context.fail!(errors: "internal server error") unless context.profiles
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class ShowUser < StandardInteraction
class ShowProfile < StandardInteraction
def validate_input
context.fail!(error: "invalid user id") unless context.id
end
Expand Down
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
Rails.application.routes.draw do
mount Knock::Engine => "/signin"

resources :users, except: [:new, :edit]
resource :user, except: [:new, :edit]
resources :password_resets, only: [:create, :update]
resources :groups, except: [:new, :edit]
resources :profiles, only: [:show, :index]
end
106 changes: 106 additions & 0 deletions spec/controllers/profiles_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
RSpec.describe ProfilesController do
describe "GET #show" do
let(:user) { create(:confirmed_user) }
let(:params) { { id: user.id } }
let(:show_profile_input) { { id: params.fetch(:id).to_s } }

let(:show_profile_context) do
Interactor::Context.new(errors: :val, user: user)
end

before(:example) do
allow(ShowProfile).to receive(:call).with(show_profile_input).
and_return(show_profile_context)
end

before do
authenticate
end

context "when succesful" do
it "calls the ShowProfile interactor" do
expect(ShowProfile).to receive(:call)
get :show, params
end

it "returns HTTP status 200" do
get :show, params
expect(response).to have_http_status(200)
end

it "render the user as JSON" do
get :show, params
expect(serialize(user)).to eq(response.body)
end
end

context "when ShowProfile fails" do
let(:show_profile_context) do
double(:context, errors: "invalid", success?: false)
end

it "returns HTTP status 404" do
get :show, params
expect(response).to have_http_status(404)
end

it "renders an error" do
get :show, params
expect(response.body).to eq("invalid")
end
end
end

describe "GET #index" do
let(:user) { create(:confirmed_user) }
let(:user2) { create(:confirmed_user) }
let(:user3) { create(:confirmed_user) }

let(:index_profile_context) do
Interactor::Context.new(errors: :val,
profiles: [user, user2, user3])
end

before(:example) do
allow(IndexProfile).to receive(:call).and_return(index_profile_context)
end

before do
authenticate
end

context "when succesful" do
it "calls the IndexProfile interactor" do
expect(IndexProfile).to receive(:call)
get :index
end

it "returns HTTP status 200" do
get :index
expect(response).to have_http_status(200)
end

it "render the profiles as JSON" do
get :index
expect(response.body).
to eq([serialize(user), serialize(user2), serialize(user3)])
end
end

context "when IndexProfile fails" do
let(:index_profile_context) do
double(:context, errors: "internal server error", success?: false)
end

it "returns HTTP status 500" do
get :index
expect(response).to have_http_status(500)
end

it "renders an error" do
get :index
expect(response.body).to eq("internal server error")
end
end
end
end
52 changes: 0 additions & 52 deletions spec/controllers/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,58 +81,6 @@
end
end

describe "GET #show" do
let(:user) { create(:confirmed_user) }
let(:params) { { id: user.id } }
let(:show_user_input) { { id: params.fetch(:id).to_s } }

let(:show_user_context) do
Interactor::Context.new(errors: :val, user: user)
end

before(:example) do
allow(ShowUser).to receive(:call).with(show_user_input).
and_return(show_user_context)
end

before do
authenticate
end

context "when succesful" do
it "calls the ShowUser interactor" do
expect(ShowUser).to receive(:call)
get :show, params
end

it "returns HTTP status 200" do
get :show, params
expect(response).to have_http_status(200)
end

it "render the user as JSON" do
get :show, params
expect(serialize(user)).to eq(response.body)
end
end

context "when ShowUser fails" do
let(:show_user_context) do
double(:context, errors: "invalid", success?: false)
end

it "returns HTTP status 404" do
get :show, params
expect(response).to have_http_status(404)
end

it "renders an error" do
get :show, params
expect(response.body).to eq("invalid")
end
end
end

describe "DELETE #destroy" do
let(:user) { create(:confirmed_user) }
let(:params) { { id: user.id } }
Expand Down
35 changes: 35 additions & 0 deletions spec/interactors/index_profile_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
RSpec.describe IndexProfile do
describe ".call" do
subject do
described_class.call
end

let!(:user1) { create(:confirmed_user) }
let!(:user2) { create(:confirmed_user) }
let!(:user3) { create(:confirmed_user) }

context "when successful" do
it "returns a successful context" do
is_expected.to be_a_success
end

it "sets profiles to an array of users" do
expect(subject.profiles).to eq([user1, user2, user3])
end
end

context "when unsuccessful" do
before do
allow(User).to receive(:all).and_return(nil)
end

it "fails" do
is_expected.to be_a_failure
end

it "returns an error" do
expect(subject.errors).to eq("internal server error")
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RSpec.describe ShowUser do
RSpec.describe ShowProfile do
describe ".call" do
let(:user) { create(:confirmed_user) }

Expand Down

0 comments on commit 8669034

Please sign in to comment.