Skip to content

Instantly share code, notes, and snippets.

@walter
Created June 16, 2016 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walter/020b9b5ceec6cc6d891a276b2aa62764 to your computer and use it in GitHub Desktop.
Save walter/020b9b5ceec6cc6d891a276b2aa62764 to your computer and use it in GitHub Desktop.
defmodule CubspotPhoenix.UserFromAuthSpec do
use ESpec.Phoenix, model: CubspotPhoenix.User
import Ecto.Query
import CubspotPhoenix.Factory
alias CubspotPhoenix.User
alias CubspotPhoenix.AuthProviderAuthorization
alias CubspotPhoenix.UserFromAuth
alias Ueberauth.Auth
alias Ueberauth.Auth.Credentials
alias Ueberauth.Auth.Info
let :name, do: "Bob Dobbs"
let :email, do: "bob@example.com"
let :uid, do: "hassox"
let :provider, do: :github
let :token, do: "the-token"
let :refresh_token, do: "refresh-token"
before do
auth = %Auth{
uid: uid,
provider: provider,
info: %Info{
name: name,
email: email,
},
credentials: %Credentials{
token: token,
refresh_token: "refresh-token",
expires_at: Guardian.Utils.timestamp + 1000,
}
}
{:shared, auth: auth}
end
def user_count, do: Repo.one(from u in User, select: count(u.id))
def authorization_count, do: Repo.one(from a in AuthProviderAuthorization, select: count(a.id))
context "when there neither authorization or user" do
it "creates a new authorization and user" do
before_users = user_count
before_authorizations = authorization_count
{:ok, user} = UserFromAuth.get_or_insert(shared.auth, nil, Repo)
expect user_count |> to(eq(before_users + 1))
expect authorization_count |> to(eq(before_authorizations + 1))
expect user.email |> to(eq email)
end
end
context "when user exists" do
before do
{:shared, user: create(:user, %{name: name, email: email})}
end
context "and authorization also exists" do
it "returns the existing user" do
{:ok, _authorization} = AuthProviderAuthorization.changeset(
Ecto.build_assoc(shared.user, :auth_provider_authorizations),
%{
provider: to_string(provider),
uid: uid,
token: token,
refresh_token: refresh_token,
expires_at: Guardian.Utils.timestamp + 500
}
) |> Repo.insert
before_users = user_count
before_authorizations = authorization_count
{:ok, user_from_auth} = UserFromAuth.get_or_insert(shared.auth, nil, Repo)
expect user_from_auth.id |> to(eq shared.user.id)
expect user_count |> to(eq before_users)
expect authorization_count |> to(eq before_authorizations)
end
end
context "and user has the same email" do
it "returns existing user and creates authorization" do
before_users = user_count
before_authorizations = authorization_count
{:ok, user_from_auth} = UserFromAuth.get_or_insert(shared.auth, nil, Repo)
expect user_from_auth.id |> to(eq shared.user.id)
expect user_count |> to(eq before_users)
expect authorization_count |> to(eq(before_authorizations + 1))
end
end
context "and old authorization is expired" do
it "deletes the authorization and makes a new one" do
{:ok, authorization} = AuthProviderAuthorization.changeset(
Ecto.build_assoc(shared.user, :auth_provider_authorizations),
%{
provider: to_string(provider),
uid: uid,
token: token,
refresh_token: refresh_token,
expires_at: Guardian.Utils.timestamp - 500
}
) |> Repo.insert
before_users = user_count
before_authorizations = authorization_count
{:ok, user_from_auth} = UserFromAuth.get_or_insert(shared.auth, nil, Repo)
expect user_from_auth.id |> to(eq shared.user.id)
expect user_count |> to(eq before_users)
expect authorization_count |> to(eq before_authorizations)
auth2 = Repo.one(Ecto.assoc(shared.user, :auth_provider_authorizations))
expect auth2.id |> not_to(eq authorization.id)
end
end
context "if the user is not current user" do
it "returns an error" do
{:ok, current_user} = create(:user, %{email: "fred@example.com"})
{:ok, _authorization} = AuthProviderAuthorization.changeset(
Ecto.build_assoc(shared.user, :auth_provider_authorizations),
%{
provider: to_string(provider),
uid: uid,
token: token,
refresh_token: refresh_token,
expires_at: Guardian.Utils.timestamp + 500
}
) |> Repo.insert
{:error, :user_does_not_match} = UserFromAuth.get_or_insert(shared.auth, current_user, Repo)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment