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

Commit 58b7b7e

Browse files
committed
chore: Merge branch 'git-repo' into dev
2 parents aec1335 + 26211e0 commit 58b7b7e

File tree

3 files changed

+136
-28
lines changed

3 files changed

+136
-28
lines changed

lib/groupher_server_web/schema/cms/mutations/repo.ex

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,37 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Repo do
3838
middleware(M.Statistics.MakeContribute, for: [:user, :community])
3939
end
4040

41+
@desc "update a cms/repo"
42+
field :update_repo, :repo do
43+
arg(:id, non_null(:id))
44+
arg(:title, :string)
45+
arg(:owner_name, :string)
46+
arg(:owner_url, :string)
47+
arg(:repo_url, :string)
48+
49+
arg(:desc, :string)
50+
arg(:homepage_url, :string)
51+
arg(:readme, :string)
52+
53+
arg(:star_count, :integer)
54+
arg(:issues_count, :integer)
55+
arg(:prs_count, :integer)
56+
arg(:fork_count, :integer)
57+
arg(:watch_count, :integer)
58+
59+
arg(:license, :string)
60+
arg(:release_tag, :string)
61+
62+
arg(:contributors, list_of(:repo_contributor_input))
63+
arg(:primary_language, :repo_lang_input)
64+
65+
middleware(M.Authorize, :login)
66+
middleware(M.PassportLoader, source: :repo)
67+
middleware(M.Passport, claim: "owner;cms->c?->repo.edit")
68+
69+
resolve(&R.CMS.update_content/3)
70+
end
71+
4172
@desc "pin a repo"
4273
field :pin_repo, :repo do
4374
arg(:id, non_null(:id))
@@ -98,26 +129,5 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Repo do
98129

99130
resolve(&R.CMS.delete_content/3)
100131
end
101-
102-
@desc "update a cms/repo"
103-
field :update_repo, :repo do
104-
arg(:repo_name, non_null(:string))
105-
arg(:desc, non_null(:string))
106-
arg(:readme, non_null(:string))
107-
arg(:language, non_null(:string))
108-
arg(:repo_link, non_null(:string))
109-
arg(:producer, non_null(:string))
110-
arg(:producer_link, non_null(:string))
111-
112-
arg(:repo_star_count, non_null(:integer))
113-
arg(:repo_fork_count, non_null(:integer))
114-
arg(:repo_watch_count, non_null(:integer))
115-
116-
middleware(M.Authorize, :login)
117-
middleware(M.PassportLoader, source: :repo)
118-
middleware(M.Passport, claim: "owner;cms->c?->repo.edit")
119-
120-
resolve(&R.CMS.update_content/3)
121-
end
122132
end
123133
end

test/groupher_server_web/mutation/cms/repo_test.exs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,95 @@ defmodule GroupherServer.Test.Mutation.Repo do
8282
assert created["origialCommunity"]["id"] == to_string(community.id)
8383
assert {:ok, _} = ORM.find_by(CMS.Author, user_id: user.id)
8484
end
85+
86+
@update_repo_query """
87+
mutation(
88+
$id: ID!,
89+
$title: String,
90+
$ownerName: String,
91+
$ownerUrl: String,
92+
$repoUrl: String,
93+
$desc: String,
94+
$homepageUrl: String,
95+
$readme: String,
96+
$starCount: Int,
97+
$issuesCount: Int,
98+
$prsCount: Int,
99+
$forkCount: Int,
100+
$watchCount: Int,
101+
$license: String,
102+
$releaseTag: String,
103+
$primaryLanguage: RepoLangInput,
104+
$contributors: [RepoContributorInput],
105+
) {
106+
updateRepo(
107+
id: $id,
108+
title: $title,
109+
ownerName: $ownerName,
110+
ownerUrl: $ownerUrl,
111+
repoUrl: $repoUrl,
112+
desc: $desc,
113+
homepageUrl: $homepageUrl,
114+
readme: $readme,
115+
starCount: $starCount,
116+
issuesCount: $issuesCount,
117+
prsCount: $prsCount,
118+
forkCount: $forkCount,
119+
watchCount: $watchCount,
120+
primaryLanguage: $primaryLanguage,
121+
license: $license,
122+
releaseTag: $releaseTag,
123+
contributors: $contributors,
124+
) {
125+
id
126+
title
127+
readme
128+
desc
129+
origialCommunity {
130+
id
131+
}
132+
}
133+
}
134+
"""
135+
test "update git-repo with valid attrs" do
136+
{:ok, user} = db_insert(:user)
137+
user_conn = simu_conn(:user, user)
138+
{:ok, community} = db_insert(:community)
139+
repo_attr = mock_attrs(:repo) |> camelize_map_key
140+
variables = repo_attr |> Map.merge(%{communityId: community.id})
141+
created = user_conn |> mutation_result(@create_repo_query, variables, "createRepo")
142+
{:ok, repo} = ORM.find(CMS.Repo, created["id"])
143+
144+
updated =
145+
user_conn
146+
|> mutation_result(
147+
@update_repo_query,
148+
%{id: repo.id, title: "new title", readme: "new readme"},
149+
"updateRepo"
150+
)
151+
152+
# IO.inspect(updated, label: "hello")
153+
assert updated["title"] == "new title"
154+
assert updated["readme"] == "new readme"
155+
end
156+
157+
test "unauth user update git-repo fails", ~m(user_conn guest_conn repo)a do
158+
unique_num = System.unique_integer([:positive, :monotonic])
159+
160+
variables = %{
161+
id: repo.id,
162+
title: "updated title #{unique_num}",
163+
body: "updated body #{unique_num}"
164+
}
165+
166+
rule_conn = simu_conn(:user, cms: %{"what.ever" => true})
167+
168+
assert user_conn |> mutation_get_error?(@update_repo_query, variables, ecode(:passport))
169+
170+
assert guest_conn
171+
|> mutation_get_error?(@update_repo_query, variables, ecode(:account_login))
172+
173+
assert rule_conn |> mutation_get_error?(@update_repo_query, variables, ecode(:passport))
174+
end
85175
end
86176
end

test/support/assert_helper.ex

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,24 +87,25 @@ defmodule GroupherServer.Test.AssertHelper do
8787
@doc """
8888
simulate the Graphiql murate operation
8989
"""
90-
def mutation_result(conn, query, variables, key) do
90+
def mutation_result(conn, query, variables, key, flag \\ false) do
9191
conn
9292
|> post("/graphiql", query: query, variables: variables)
9393
|> json_response(200)
94-
# |> IO.inspect(label: "debug")
94+
|> log_debug_info(flag)
9595
|> Map.get("data")
9696
|> Map.get(key)
9797
end
9898

9999
@doc """
100100
check if Graphiql murate get error
101101
"""
102-
def mutation_get_error?(conn, query, variables) do
102+
def mutation_get_error?(conn, query, variables, flag \\ false)
103+
104+
def mutation_get_error?(conn, query, variables, flag) do
103105
conn
104106
|> post("/graphiql", query: query, variables: variables)
105-
# |> IO.inspect(label: "debug status")
106107
|> json_response(200)
107-
# |> IO.inspect(label: "debug")
108+
|> log_debug_info(flag)
108109
|> Map.has_key?("errors")
109110
end
110111

@@ -128,11 +129,11 @@ defmodule GroupherServer.Test.AssertHelper do
128129
end
129130
end
130131

131-
def query_result(conn, query, variables, key) do
132+
def query_result(conn, query, variables, key, flag \\ false) do
132133
conn
133134
|> get("/graphiql", query: query, variables: variables)
134135
|> json_response(200)
135-
# |> IO.inspect(label: "debug")
136+
|> log_debug_info(flag)
136137
|> Map.get("data")
137138
|> Map.get(key)
138139
end
@@ -176,4 +177,11 @@ defmodule GroupherServer.Test.AssertHelper do
176177

177178
[value_1, value_2, value_3, value_x]
178179
end
180+
181+
# log response info if need
182+
# usage:
183+
# user_conn |> mutation_result(@query, variables, "createRepo")
184+
# user_conn |> mutation_result(@query, variables, "createRepo", :debug)
185+
defp log_debug_info(res, :debug), do: IO.inspect(res, label: "debug")
186+
defp log_debug_info(res, _), do: res
179187
end

0 commit comments

Comments
 (0)