-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
181 additions
and
70 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 |
---|---|---|
@@ -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 |
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,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 |
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 |
---|---|---|
@@ -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 |
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,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 |
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,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 |
2 changes: 1 addition & 1 deletion
2
spec/interactors/show_user_spec.rb → spec/interactors/show_profile_spec.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