-
Notifications
You must be signed in to change notification settings - Fork 0
/
register_farm_admin_test.exs
56 lines (48 loc) · 1.56 KB
/
register_farm_admin_test.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
defmodule NomNomsFarm.GraphQL.RegisterFarmAdminTest do
use ExUnit.Case
alias Ecto.Adapters.SQL.Sandbox
alias NomNomsFarm.{Farm, Farmer, Repo, User, UsdaFarm}
setup do
:ok = Sandbox.checkout(Repo)
add_test_data_to_db()
Sandbox.mode(Repo, {:shared, self()})
end
def add_test_data_to_db do
Repo.insert!(%UsdaFarm{id: 1, name: "Bartleby & Son's", address: "400 Plainfield", usda_uid: "00920"})
end
test "successfully registers a farm admin" do
mutation = """
mutation {
registerFarmAdmin(
username: "bart",
password: "password",
name: "Bartleby",
email: "bart@bartlebysons.com",
usdaUid: "00920",
)
}
"""
assert {:ok, result} = Absinthe.run(mutation, NomNomsFarm.Web.Schema)
refute Map.has_key?(result, :errors)
%{data: %{"registerFarmAdmin" => user_id}} = result
created_user = Repo.get(User, user_id)
assert created_user.username == "bart"
end
test "returns a helpful error message when request fails" do
Repo.insert!(%User{email: "bart@bartlebysons.com"})
mutation = """
mutation {
registerFarmAdmin(
username: "bart",
password: "password",
name: "Bartleby",
email: "bart@bartlebysons.com",
usdaUid: "00920",
)
}
"""
assert {:ok, %{errors: [error| _]}} = Absinthe.run(mutation, NomNomsFarm.Web.Schema)
expected_message = NomNomsFarm.error_message(:email_already_claimed)
assert String.contains?(error.message, expected_message)
end
end