-
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.
✨ create Users::FindByUsername operation
- Loading branch information
1 parent
404f215
commit 161656a
Showing
3 changed files
with
58 additions
and
2 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
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,17 @@ | ||
# frozen_string_literal: true | ||
|
||
module Users | ||
class FindByUsername < Actor | ||
input :username, type: String | ||
|
||
output :user, type: User | ||
|
||
def call | ||
profile = UserProfile.find_by(username: username) | ||
|
||
fail!(error: :not_found) if profile.blank? | ||
|
||
self.user = profile.user | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe Users::FindByUsername, type: :operation do | ||
context "when user with given username exists" do | ||
it "is a success" do | ||
user = create(:user) | ||
create(:user_profile, username: "johndoe", user: user) | ||
|
||
result = described_class.result(username: "johndoe") | ||
|
||
expect(result.success?).to be true | ||
end | ||
|
||
it "returns found user for given username" do | ||
user = create(:user) | ||
create(:user_profile, username: "johndoe", user: user) | ||
|
||
result = described_class.result(username: "johndoe") | ||
|
||
expect(result.user).to eq(user) | ||
end | ||
end | ||
|
||
context "when user with given username doesn't exist" do | ||
it "is a failure" do | ||
result = described_class.result(username: "not-found-username") | ||
|
||
expect(result.failure?).to be true | ||
end | ||
|
||
it "returns not found error" do | ||
result = described_class.result(username: "not-found-username") | ||
|
||
expect(result.error).to be :not_found | ||
end | ||
end | ||
end |