-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathproject_controller_test.exs
More file actions
112 lines (94 loc) · 3.39 KB
/
project_controller_test.exs
File metadata and controls
112 lines (94 loc) · 3.39 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
defmodule LightningWeb.API.ProjectControllerTest do
use LightningWeb.ConnCase, async: true
import Lightning.Factories
setup %{conn: conn} do
{:ok, conn: put_req_header(conn, "accept", "application/json")}
end
describe "without a token" do
test "gets a 401", %{conn: conn} do
conn = get(conn, ~p"/api/projects")
assert json_response(conn, 401) == %{"error" => "Unauthorized"}
end
end
describe "with invalid token" do
test "gets a 401", %{conn: conn} do
token = "Oooops"
conn = conn |> Plug.Conn.put_req_header("authorization", "Bearer #{token}")
conn = get(conn, ~p"/api/projects")
assert json_response(conn, 401) == %{"error" => "Unauthorized"}
end
end
describe "index" do
setup [:assign_bearer_for_api, :create_project_for_current_user]
test "lists all projects i belong to", %{conn: conn, project: project} do
conn = get(conn, ~p"/api/projects")
response = json_response(conn, 200)
assert response["data"] == [
%{
"attributes" => %{
"name" => project.name,
"description" => nil
},
"id" => project.id,
"links" => %{
"self" => "http://localhost:4002/api/projects/#{project.id}"
},
"relationships" => %{},
"type" => "projects"
}
]
end
test "Other user don't have access to user project", %{
conn: conn,
project: project
} do
other_user = insert(:user)
token =
other_user
|> Lightning.Accounts.generate_api_token()
conn = conn |> Plug.Conn.put_req_header("authorization", "Bearer #{token}")
insert(:project, project_users: [%{user_id: other_user.id}])
conn = get(conn, ~p"/api/projects")
response = json_response(conn, 200)
refute response["data"] == [
%{
"attributes" => %{"name" => "a-test-project"},
"id" => project.id,
"links" => %{
"self" => "http://localhost:4002/api/projects/#{project.id}"
},
"relationships" => %{},
"type" => "projects"
}
]
end
end
describe "show" do
setup [:assign_bearer_for_api, :create_project_for_current_user]
test "with token for other project", %{conn: conn} do
other_project = insert(:project)
conn = get(conn, ~p"/api/projects/#{other_project.id}")
assert json_response(conn, 401) == %{"error" => "Unauthorized"}
end
test "returns 400 for malformed id", %{conn: conn} do
conn = get(conn, ~p"/api/projects/not-a-uuid")
assert json_response(conn, 400) == %{"error" => "Bad Request"}
end
test "shows the project", %{conn: conn, project: project} do
conn = get(conn, Routes.api_project_path(conn, :show, project))
response = json_response(conn, 200)
assert response["data"] == %{
"attributes" => %{
"name" => project.name,
"description" => nil
},
"id" => project.id,
"links" => %{
"self" => "http://localhost:4002/api/projects/#{project.id}"
},
"relationships" => %{},
"type" => "projects"
}
end
end
end