Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit 59d3156

Browse files
committed
refactor(tests): use assert_v as namespace for test constants
1 parent ef0ef1e commit 59d3156

File tree

7 files changed

+34
-24
lines changed

7 files changed

+34
-24
lines changed

test/groupher_server_web/mutation/cms/job_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,13 @@ defmodule GroupherServer.Test.Mutation.Job do
9898
user_conn = simu_conn(:user, user)
9999

100100
{:ok, community} = db_insert(:community)
101-
job_attr = mock_attrs(:job, %{body: xss_string()})
101+
job_attr = mock_attrs(:job, %{body: assert_v(:xss_string)})
102102

103103
variables = job_attr |> Map.merge(%{communityId: community.id}) |> camelize_map_key
104104
created = user_conn |> mutation_result(@create_job_query, variables, "createJob")
105105
{:ok, job} = ORM.find(CMS.Job, created["id"])
106106

107-
assert job.body == xss_safe_string()
107+
assert job.body == assert_v(:xss_safe_string)
108108
end
109109

110110
test "can create job with tags" do

test/groupher_server_web/mutation/cms/post_comment_test.exs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,19 @@ defmodule GroupherServer.Test.Mutation.PostComment do
5050

5151
@tag :wip
5252
test "xss comment should be escaped", ~m(user_conn community post)a do
53-
variables = %{community: community.raw, thread: "POST", id: post.id, body: xss_string()}
53+
variables = %{
54+
community: community.raw,
55+
thread: "POST",
56+
id: post.id,
57+
body: assert_v(:xss_string)
58+
}
59+
5460
created = user_conn |> mutation_result(@create_comment_query, variables, "createComment")
5561

5662
{:ok, found} = ORM.find(CMS.PostComment, created["id"])
5763

5864
assert created["id"] == to_string(found.id)
59-
assert created["body"] == xss_safe_string()
65+
assert created["body"] == assert_v(:xss_safe_string)
6066
end
6167

6268
test "guest user create comment fails", ~m(guest_conn post community)a do

test/groupher_server_web/mutation/cms/post_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ defmodule GroupherServer.Test.Mutation.Post do
6969
user_conn = simu_conn(:user, user)
7070

7171
{:ok, community} = db_insert(:community)
72-
post_attr = mock_attrs(:post, %{body: xss_string()})
72+
post_attr = mock_attrs(:post, %{body: assert_v(:xss_string)})
7373

7474
variables = post_attr |> Map.merge(%{communityId: community.id})
7575
created = user_conn |> mutation_result(@create_post_query, variables, "createPost")
7676
{:ok, post} = ORM.find(CMS.Post, created["id"])
7777

78-
assert post.body == xss_safe_string()
78+
assert post.body == assert_v(:xss_safe_string)
7979
end
8080

8181
# NOTE: this test is IMPORTANT, cause json_codec: Jason in router will cause

test/groupher_server_web/mutation/cms/repo_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,13 @@ defmodule GroupherServer.Test.Mutation.Repo do
160160
user_conn = simu_conn(:user, user)
161161

162162
{:ok, community} = db_insert(:community)
163-
repo_attr = mock_attrs(:repo, %{readme: xss_string()})
163+
repo_attr = mock_attrs(:repo, %{readme: assert_v(:xss_string)})
164164

165165
variables = repo_attr |> Map.merge(%{communityId: community.id}) |> camelize_map_key
166166
created = user_conn |> mutation_result(@create_repo_query, variables, "createRepo")
167167
{:ok, repo} = ORM.find(CMS.Repo, created["id"])
168168

169-
assert repo.readme == xss_safe_string()
169+
assert repo.readme == assert_v(:xss_safe_string)
170170
end
171171

172172
test "unauth user update git-repo fails", ~m(user_conn guest_conn repo)a do

test/groupher_server_web/query/accounts/account_test.exs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ defmodule GroupherServer.Test.Query.Account.Basic do
186186
"""
187187
test "guest user can get subscrubed communities list and count", ~m(guest_conn user)a do
188188
variables = %{login: user.login}
189-
{:ok, communities} = db_insert_multi(:community, page_size())
189+
{:ok, communities} = db_insert_multi(:community, assert_v(:page_size))
190190

191191
Enum.each(
192192
communities,
@@ -205,12 +205,12 @@ defmodule GroupherServer.Test.Query.Account.Basic do
205205
assert subscribed_communities |> Enum.any?(&(&1["id"] == to_string(community_2.id)))
206206
assert subscribed_communities |> Enum.any?(&(&1["id"] == to_string(community_3.id)))
207207
assert subscribed_communities |> Enum.any?(&(&1["id"] == to_string(community_x.id)))
208-
assert subscribed_communities_count == page_size()
208+
assert subscribed_communities_count == assert_v(:page_size)
209209
end
210210

211211
test "guest user can get subscrubed community list by index", ~m(guest_conn user)a do
212212
variables = %{login: user.login}
213-
{:ok, communities} = db_insert_multi(:community, page_size())
213+
{:ok, communities} = db_insert_multi(:community, assert_v(:page_size))
214214

215215
Enum.each(
216216
communities,
@@ -247,7 +247,7 @@ defmodule GroupherServer.Test.Query.Account.Basic do
247247

248248
test "guest user can get subscrubed communities count of 20 at most", ~m(guest_conn user)a do
249249
variables = %{login: user.login}
250-
{:ok, communities} = db_insert_multi(:community, page_size() + 1)
250+
{:ok, communities} = db_insert_multi(:community, assert_v(:page_size) + 1)
251251

252252
Enum.each(
253253
communities,
@@ -257,8 +257,8 @@ defmodule GroupherServer.Test.Query.Account.Basic do
257257
results = guest_conn |> query_result(@query, variables, "user")
258258
subscribed_communities = results["subscribedCommunities"]
259259

260-
assert subscribed_communities["totalCount"] == page_size() + 1
261-
assert subscribed_communities["pageSize"] == page_size()
260+
assert subscribed_communities["totalCount"] == assert_v(:page_size) + 1
261+
assert subscribed_communities["pageSize"] == assert_v(:page_size)
262262
end
263263

264264
@query """

test/groupher_server_web/query/cms/cms_test.exs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ defmodule GroupherServer.Test.Query.CMS.Basic do
443443
"""
444444
test "guest can get editors list and count of a community", ~m(guest_conn community)a do
445445
title = "chief editor"
446-
{:ok, users} = db_insert_multi(:user, inner_page_size())
446+
{:ok, users} = db_insert_multi(:user, assert_v(:inner_page_size))
447447

448448
Enum.each(
449449
users,
@@ -462,7 +462,7 @@ defmodule GroupherServer.Test.Query.CMS.Basic do
462462
assert editors |> Enum.any?(&(&1["id"] == to_string(user_2.id)))
463463
assert editors |> Enum.any?(&(&1["id"] == to_string(user_3.id)))
464464
assert editors |> Enum.any?(&(&1["id"] == to_string(user_x.id)))
465-
assert editors_count == inner_page_size()
465+
assert editors_count == assert_v(:inner_page_size)
466466
end
467467

468468
@query """
@@ -508,7 +508,7 @@ defmodule GroupherServer.Test.Query.CMS.Basic do
508508
}
509509
"""
510510
test "guest can get subscribers list and count of a community", ~m(guest_conn community)a do
511-
{:ok, users} = db_insert_multi(:user, inner_page_size())
511+
{:ok, users} = db_insert_multi(:user, assert_v(:inner_page_size))
512512

513513
Enum.each(
514514
users,
@@ -527,11 +527,11 @@ defmodule GroupherServer.Test.Query.CMS.Basic do
527527
assert subscribers |> Enum.any?(&(&1["id"] == to_string(user_2.id)))
528528
assert subscribers |> Enum.any?(&(&1["id"] == to_string(user_3.id)))
529529
assert subscribers |> Enum.any?(&(&1["id"] == to_string(user_x.id)))
530-
assert subscribers_count == inner_page_size()
530+
assert subscribers_count == assert_v(:inner_page_size)
531531
end
532532

533533
test "guest user can get subscribers count of 20 at most", ~m(guest_conn community)a do
534-
{:ok, users} = db_insert_multi(:user, inner_page_size() + 1)
534+
{:ok, users} = db_insert_multi(:user, assert_v(:inner_page_size) + 1)
535535

536536
Enum.each(
537537
users,
@@ -542,7 +542,7 @@ defmodule GroupherServer.Test.Query.CMS.Basic do
542542
results = guest_conn |> query_result(@query, variables, "community")
543543
subscribers = results["subscribers"]
544544

545-
assert length(subscribers) == inner_page_size()
545+
assert length(subscribers) == assert_v(:inner_page_size)
546546
end
547547

548548
@query """

test/support/assert_helper.ex

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ defmodule GroupherServer.Test.AssertHelper do
1616
used for non exsit id
1717
"""
1818
def non_exsit_id, do: 15_982_398_614
19-
def inner_page_size, do: @inner_page_size
20-
def page_size, do: @page_size
19+
# def page_size, do: @page_size
2120

22-
def xss_string, do: "<script>alert(\"hello,world\")</script>"
23-
def xss_safe_string, do: "&lt;script&gt;alert(&quot;hello,world&quot;)&lt;/script&gt;"
21+
def assert_v(:inner_page_size), do: @inner_page_size
22+
def assert_v(:page_size), do: @page_size
23+
24+
def assert_v(:xss_string), do: "<script>alert(\"hello,world\")</script>"
25+
26+
def assert_v(:xss_safe_string),
27+
do: "&lt;script&gt;alert(&quot;hello,world&quot;)&lt;/script&gt;"
2428

2529
def is_valid_kv?(obj, key, :list) when is_map(obj) do
2630
obj = map_key_stringify(obj)

0 commit comments

Comments
 (0)