forked from code-corps/code-corps-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_controller_test.exs
More file actions
290 lines (244 loc) · 8.45 KB
/
user_controller_test.exs
File metadata and controls
290 lines (244 loc) · 8.45 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
defmodule CodeCorps.UserControllerTest do
use CodeCorps.ApiCase
alias CodeCorps.User
alias CodeCorps.Repo
alias CodeCorps.SluggedRoute
@valid_attrs %{
email: "test@user.com",
username: "testuser",
first_name: "Test",
last_name: "User",
website: "http://www.example.com",
twitter: "testuser",
biography: "Just a test user"
}
@invalid_attrs %{
email: "",
username: "",
website: "---_<>-blank.com",
twitter: " @ testuser"
}
defp relationships do
%{}
end
describe "index" do
test "lists all entries on index", %{conn: conn} do
conn = get conn, user_path(conn, :index)
assert json_response(conn, 200)["data"] == []
end
test "filters resources on index", %{conn: conn} do
user_1 = insert(:user, username: "user_1", email: "user_1@mail.com")
user_2 = insert(:user, username: "user_2", email: "user_2@mail.com")
insert(:user, username: "user_3", email: "user_3@mail.com")
conn = get conn, "users/?filter[id]=#{user_1.id},#{user_2.id}"
data = json_response(conn, 200)["data"]
[first_result, second_result | _] = data
assert length(data) == 2
assert first_result["id"] == "#{user_1.id}"
assert second_result["id"] == "#{user_2.id}"
end
end
describe "#show" do
test "shows chosen resource", %{conn: conn} do
user = insert(:user)
conn = get conn, user_path(conn, :show, user)
data = json_response(conn, 200)["data"]
assert data["id"] == "#{user.id}"
assert data["type"] == "user"
assert data["attributes"]["username"] == user.username
assert data["attributes"]["email"] == ""
assert data["attributes"]["password"] == nil
end
test "renders email when authenticated", %{conn: conn} do
user = insert(:user)
path = conn |> user_path(:show, user)
json = conn |> authenticate(user) |> get(path) |> json_response(200)
assert json["data"]["attributes"]["email"] == user.email
end
test "does not show resource and instead throw error when id is nonexistent", %{conn: conn} do
assert_error_sent 404, fn ->
get conn, user_path(conn, :show, -1)
end
end
end
describe "create" do
test "creates and renders resource when data is valid", %{conn: conn} do
attrs = Map.put(@valid_attrs, :password, "password")
conn = post conn, user_path(conn, :create), %{
"meta" => %{},
"data" => %{
"type" => "user",
"attributes" => attrs,
"relationships" => relationships
}
}
id = json_response(conn, 201)["data"]["id"]
assert id
user = Repo.get(User, id)
assert user
slugged_route = Repo.get_by(SluggedRoute, slug: "testuser")
assert slugged_route
assert user.id == slugged_route.user_id
end
test "does not create resource and renders errors when data is invalid", %{conn: conn} do
conn = post conn, user_path(conn, :create), %{
"meta" => %{},
"data" => %{
"type" => "user",
"attributes" => @invalid_attrs,
"relationships" => relationships
}
}
assert json_response(conn, 422)["errors"] != %{}
end
end
describe "update" do
test "updates and renders chosen resource when data is valid", %{conn: conn} do
user = insert(:user)
attrs = Map.put(@valid_attrs, :password, "password")
params = %{
"meta" => %{},
"data" => %{
"type" => "user",
"id" => user.id,
"attributes" => attrs,
"relationships" => relationships
}
}
path = user_path(conn, :update, user)
conn =
conn
|> authenticate(user)
|> put(path, params)
id = json_response(conn, 200)["data"]["id"]
assert id
user = Repo.get(User, id)
assert user.email == "test@user.com"
assert user.first_name == "Test"
assert user.last_name == "User"
assert user.website == "http://www.example.com"
assert user.biography == "Just a test user"
end
test "does not update when authorized as different user", %{conn: conn} do
user = insert(:user)
another_user = insert(:user)
attrs = Map.put(@valid_attrs, :password, "password")
path = user_path(conn, :update, user)
params = %{
"meta" => %{},
"data" => %{
"type" => "user",
"id" => user.id,
"attributes" => attrs,
"relationships" => relationships
}
}
conn =
conn
|> authenticate(another_user)
|> put(path, params)
assert json_response(conn, 401)
end
@tag :requires_env
test "uploads a photo to S3", %{conn: conn} do
user = insert(:user)
photo_data = "data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs="
attrs = Map.put(@valid_attrs, :base64_photo_data, photo_data)
path = user_path(conn, :update, user)
params = %{
"meta" => %{},
"data" => %{
"type" => "user",
"id" => user.id,
"attributes" => attrs,
"relationships" => relationships
}
}
conn =
conn
|> authenticate(user)
|> put(path, params)
data = json_response(conn, 200)["data"]
large_url = data["attributes"]["photo-large-url"]
assert String.contains? large_url, "/users/#{user.id}/large"
thumb_url = data["attributes"]["photo-thumb-url"]
assert String.contains? thumb_url, "/users/#{user.id}/thumb"
end
test "does not update chosen resource and renders errors when data is invalid", %{conn: conn} do
user = insert(:user)
path = user_path(conn, :update, user)
params = %{
"meta" => %{},
"data" => %{
"type" => "user",
"id" => user.id,
"attributes" => @invalid_attrs,
"relationships" => relationships
}
}
conn =
conn
|> authenticate(user)
|> put(path, params)
json = json_response(conn, 422)
assert json["errors"] != %{}
end
test "transitions from one state to the next", %{conn: conn} do
user = insert(:user)
conn = put authenticate(conn, user), user_path(conn, :update, user), %{
"data" => %{
"type" => "user",
"id" => user.id,
"attributes" => %{"password" => "password", "state_transition" => "edit_profile"}
}
}
%{"data" => %{"id" => id}} = json_response(conn, 200)
user = Repo.get(User, id)
assert user.state == "edited_profile"
# Transition was successful, so we should unset it
assert user.state_transition == nil
end
end
describe "email_available" do
test "returns valid and available when email is valid and available", %{conn: conn} do
resp = get conn, user_path(conn, :email_available, %{email: "available@mail.com"})
json = json_response(resp, 200)
assert json["available"]
assert json["valid"]
end
test "returns valid but inavailable when email is valid but taken", %{conn: conn} do
insert(:user, email: "used@mail.com")
resp = get conn, user_path(conn, :email_available, %{email: "used@mail.com"})
json = json_response(resp, 200)
refute json["available"]
assert json["valid"]
end
test "returns as available but invalid when email is invalid", %{conn: conn} do
resp = get conn, user_path(conn, :email_available, %{email: "not_an_email"})
json = json_response(resp, 200)
assert json["available"]
refute json["valid"]
end
end
describe "username_available" do
test "returns as valid and available when username is valid and available", %{conn: conn} do
resp = get conn, user_path(conn, :username_available, %{username: "available"})
json = json_response(resp, 200)
assert json["available"]
assert json["valid"]
end
test "returns as valid, but inavailable when username is valid but taken", %{conn: conn} do
insert(:user, username: "used")
resp = get conn, user_path(conn, :username_available, %{username: "used"})
json = json_response(resp, 200)
refute json["available"]
assert json["valid"]
end
test "returns available but invalid when username is invalid", %{conn: conn} do
resp = get conn, user_path(conn, :username_available, %{username: ""})
json = json_response(resp, 200)
assert json["available"]
refute json["valid"]
end
end
end