-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added "github" context to users created during github sync
- added Accounts bounded context - added Accounts.create_from_github - added AccountsTest - added user.json github fixture
- Loading branch information
Showing
4 changed files
with
60 additions
and
20 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,26 @@ | ||
defmodule CodeCorps.Accounts do | ||
@moduledoc ~S""" | ||
Main entry-point for managing accounts. | ||
All actions to acounts should go through here. | ||
""" | ||
|
||
alias CodeCorps.{ | ||
GitHub.Adapters, | ||
User, | ||
Repo | ||
} | ||
alias Ecto.Changeset | ||
|
||
@doc ~S""" | ||
Creates a user record using attributes from a GitHub payload. | ||
""" | ||
@spec create_from_github(map) :: {:ok, User.t} | {:error, Changeset.t} | ||
def create_from_github(%{} = attrs) do | ||
%User{} | ||
|> Changeset.change(attrs |> Adapters.User.from_github_user()) | ||
|> Changeset.put_change(:context, "github") | ||
|> Changeset.unique_constraint(:email) | ||
|> Repo.insert | ||
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
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,30 @@ | ||
defmodule CodeCorps.AccountsTest do | ||
@moduledoc false | ||
|
||
use CodeCorps.DbAccessCase | ||
|
||
alias CodeCorps.{Accounts, User, GitHub.TestHelpers} | ||
alias Ecto.Changeset | ||
|
||
describe "create_from_github/1" do | ||
test "creates proper user from provided payload" do | ||
{:ok, %User{} = user} = | ||
"user" | ||
|> TestHelpers.load_endpoint_fixture | ||
|> Accounts.create_from_github | ||
|
||
assert user.id | ||
assert user.context == "github" | ||
end | ||
|
||
test "returns changeset if there was a validation error" do | ||
%{"email" => email} = payload = TestHelpers.load_endpoint_fixture("user") | ||
# email must be unique, so if a user with email already exists, this | ||
# triggers a validation error | ||
insert(:user, email: email) | ||
|
||
{:error, %Changeset{} = changeset} = payload |> Accounts.create_from_github | ||
assert changeset.errors[:email] == {"has already been taken", []} | ||
end | ||
end | ||
end |