-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathjob_controller_test.exs
More file actions
112 lines (90 loc) · 3.39 KB
/
job_controller_test.exs
File metadata and controls
112 lines (90 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.JobControllerTest do
use LightningWeb.ConnCase, async: true
import Lightning.JobsFixtures
import Lightning.ProjectsFixtures
setup %{conn: conn} do
{:ok, conn: put_req_header(conn, "accept", "application/json")}
end
test "without a token", %{conn: conn} do
conn = get(conn, ~p"/api/projects/#{Ecto.UUID.generate()}/jobs")
assert %{"error" => "Unauthorized"} == json_response(conn, 401)
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/#{Ecto.UUID.generate()}/jobs")
assert json_response(conn, 401) == %{"error" => "Unauthorized"}
end
end
describe "index" do
setup [:assign_bearer_for_api, :create_project_for_current_user, :create_job]
test "lists all jobs for project I belong to", %{conn: conn, job: job} do
conn = get(conn, ~p"/api/projects/#{job.workflow.project_id}/jobs")
response = json_response(conn, 200)
assert response["data"] == [
%{
"attributes" => %{"name" => "some name"},
"id" => job.id,
"links" => %{
"self" => "http://localhost:4002/api/jobs/#{job.id}"
},
"relationships" => %{},
"type" => "jobs"
}
]
end
test "responds with a 401 when I don't have access", %{conn: conn} do
other_project = project_fixture()
conn = get(conn, ~p"/api/projects/#{other_project.id}/jobs")
response = json_response(conn, 401)
assert response == %{"error" => "Unauthorized"}
end
test "lists all jobs", %{conn: conn, job: job} do
conn = get(conn, ~p"/api/jobs")
response = json_response(conn, 200)
assert response["data"] == [
%{
"attributes" => %{"name" => "some name"},
"id" => job.id,
"links" => %{
"self" => "http://localhost:4002/api/jobs/#{job.id}"
},
"relationships" => %{},
"type" => "jobs"
}
]
end
end
describe "show" do
setup [:assign_bearer_for_api, :create_project_for_current_user, :create_job]
test "shows the job", %{conn: conn, job: job} do
conn = get(conn, ~p"/api/jobs/#{job}")
response = json_response(conn, 200)
assert response["data"] == %{
"attributes" => %{"name" => "some name"},
"id" => job.id,
"links" => %{
"self" => "http://localhost:4002/api/jobs/#{job.id}"
},
"relationships" => %{},
"type" => "jobs"
}
end
test "returns 400 for malformed id", %{conn: conn} do
conn = get(conn, ~p"/api/jobs/not-a-uuid")
assert json_response(conn, 400) == %{"error" => "Bad Request"}
end
end
describe "index with invalid project_id" do
setup [:assign_bearer_for_api]
test "returns 400 for malformed project_id", %{conn: conn} do
conn = get(conn, ~p"/api/projects/not-a-uuid/jobs")
assert json_response(conn, 400) == %{"error" => "Bad Request"}
end
end
defp create_job(%{project: project}) do
%{job: job} = workflow_job_fixture(project_id: project.id)
%{job: job}
end
end