Skip to content

Commit 915cbc0

Browse files
committed
Test outcomes on IssueComment event on normal issues
1 parent 300a292 commit 915cbc0

File tree

2 files changed

+159
-7
lines changed

2 files changed

+159
-7
lines changed

lib/code_corps/github/sync/sync.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,12 @@ defmodule CodeCorps.GitHub.Sync do
267267
{:error, :task, %Changeset{} = changeset, _steps} ->
268268
{:error, :validating_task, changeset}
269269

270-
{:error, :github_comment, %Changeset{} = changeset, _steps} ->
270+
{:error, :github_comment, %Changeset{data: %GithubComment{}} = changeset, _steps} ->
271271
{:error, :validating_github_comment, changeset}
272272

273+
{:error, :github_comment, %Changeset{data: %GithubUser{}} = changeset, _steps} ->
274+
{:error, :validating_github_user_on_github_comment, changeset}
275+
273276
{:error, :comment_user, %Changeset{} = changeset, _steps} ->
274277
{:error, :validating_comment_user, changeset}
275278

test/lib/code_corps/github/sync/sync_test.exs

Lines changed: 155 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ defmodule CodeCorps.GitHub.SyncTest do
106106
end
107107
end
108108

109-
describe "issue_comment_event/1" do
109+
describe "issue_comment_event/1 on comment created for pull request" do
110110
@issue_comment_preloads [
111111
:user,
112112
[task: :user],
@@ -115,7 +115,7 @@ defmodule CodeCorps.GitHub.SyncTest do
115115

116116
@payload load_event_fixture("issue_comment_created_on_pull_request")
117117

118-
test "syncs correctly when comment created for a pull request" do
118+
test "syncs correctly" do
119119
%{
120120
"issue" => %{
121121
"body" => issue_body,
@@ -392,8 +392,12 @@ defmodule CodeCorps.GitHub.SyncTest do
392392
assert {:error, :multiple_comment_users_match} ==
393393
@payload |> Sync.issue_comment_event()
394394
end
395+
end
396+
397+
describe "issue_comment_event/1 on comment created for regular issue" do
398+
@payload load_event_fixture("issue_comment_created")
395399

396-
test "syncs correctly when comment created for pull request" do
400+
test "syncs correctly" do
397401
%{
398402
"issue" => %{
399403
"body" => issue_body,
@@ -413,14 +417,14 @@ defmodule CodeCorps.GitHub.SyncTest do
413417
"repository" => %{
414418
"id" => repo_github_id
415419
}
416-
} = payload = load_event_fixture("issue_comment_created")
420+
} = @payload
417421

418422
project = insert(:project)
419423
insert(:github_repo, github_id: repo_github_id, project: project)
420424
insert(:task_list, project: project, done: true)
421425
insert(:task_list, project: project, inbox: true)
422426

423-
{:ok, comment} = Sync.issue_comment_event(payload)
427+
{:ok, comment} = Sync.issue_comment_event(@payload)
424428

425429
assert Repo.aggregate(GithubComment, :count, :id) == 1
426430
assert Repo.aggregate(GithubIssue, :count, :id) == 1
@@ -460,7 +464,152 @@ defmodule CodeCorps.GitHub.SyncTest do
460464
assert comment.user.github_id == comment_user_github_id
461465
end
462466

463-
test "syncs correctly on comment deleted" do
467+
test "can fail when finding repo" do
468+
assert {:error, :repo_not_found} == @payload |> Sync.issue_comment_event()
469+
end
470+
471+
test "can fail on github issue validation" do
472+
insert(:github_repo, github_id: @payload["repository"]["id"])
473+
assert {:error, :validating_github_issue, %Changeset{} = changeset} =
474+
@payload
475+
|> Kernel.put_in(["issue", "number"], nil)
476+
|> Sync.issue_comment_event()
477+
478+
refute changeset.valid?
479+
end
480+
481+
test "can fail on github user validation for github issue" do
482+
insert(:github_repo, github_id: @payload["repository"]["id"])
483+
assert {
484+
:error,
485+
:validating_github_user_on_github_issue,
486+
%Changeset{} = changeset
487+
} =
488+
@payload
489+
|> Kernel.put_in(["issue", "user", "login"], nil)
490+
|> Sync.issue_comment_event()
491+
492+
refute changeset.valid?
493+
end
494+
495+
test "can fail on task user validation" do
496+
insert(:github_repo, github_id: @payload["repository"]["id"])
497+
498+
# setup data to trigger a unique constraint
499+
email = "taken@mail.com"
500+
insert(:user, email: email)
501+
payload = @payload |> Kernel.put_in(["issue", "user", "email"], email)
502+
503+
assert {:error, :validating_task_user, %Changeset{} = changeset} =
504+
payload |> Sync.issue_comment_event()
505+
506+
refute changeset.valid?
507+
end
508+
509+
test "can fail if task matched with multiple users" do
510+
github_repo =
511+
insert(:github_repo, github_id: @payload["repository"]["id"])
512+
513+
attrs =
514+
@payload["issue"]
515+
|> Adapters.Issue.to_issue()
516+
|> Map.put(:github_repo, github_repo)
517+
518+
github_issue = insert(:github_issue, attrs)
519+
# creates a user for each task, which should never happen normally
520+
insert_pair(:task, github_issue: github_issue)
521+
522+
assert {:error, :multiple_task_users_match} ==
523+
@payload |> Sync.issue_comment_event()
524+
end
525+
526+
test "can fail on task validation" do
527+
insert(:github_repo, github_id: @payload["repository"]["id"])
528+
529+
# validation is triggered due to missing task list
530+
531+
assert {:error, :validating_task, %Changeset{} = changeset} =
532+
@payload |> Sync.issue_comment_event()
533+
534+
refute changeset.valid?
535+
end
536+
537+
test "can fail on github comment validation" do
538+
%{project: project} =
539+
insert(:github_repo, github_id: @payload["repository"]["id"])
540+
541+
insert(:task_list, project: project, done: true)
542+
insert(:task_list, project: project, inbox: true)
543+
544+
assert {:error, :validating_github_comment, %Changeset{} = changeset} =
545+
@payload
546+
|> Kernel.put_in(["comment", "url"], nil)
547+
|> Sync.issue_comment_event()
548+
549+
refute changeset.valid?
550+
end
551+
552+
test "can fail on github user validation for github comment" do
553+
%{project: project} =
554+
insert(:github_repo, github_id: @payload["repository"]["id"])
555+
556+
insert(:task_list, project: project, done: true)
557+
insert(:task_list, project: project, inbox: true)
558+
559+
assert {
560+
:error,
561+
:validating_github_user_on_github_comment,
562+
%Changeset{} = changeset
563+
} =
564+
@payload
565+
|> Kernel.put_in(["comment", "user", "login"], nil)
566+
|> Sync.issue_comment_event()
567+
568+
refute changeset.valid?
569+
end
570+
571+
test "can fail on comment user validation" do
572+
%{project: project} =
573+
insert(:github_repo, github_id: @payload["repository"]["id"])
574+
575+
insert(:task_list, project: project, done: true)
576+
insert(:task_list, project: project, inbox: true)
577+
578+
# setup data to trigger a unique constraint
579+
email = "taken@mail.com"
580+
insert(:user, email: email)
581+
582+
assert {:error, :validating_comment_user, %Changeset{} = changeset} =
583+
@payload
584+
|> Kernel.put_in(["comment", "user", "email"], email)
585+
|> Sync.issue_comment_event()
586+
587+
refute changeset.valid?
588+
end
589+
590+
test "can fail if commment matched with multiple users" do
591+
%{project: project} = github_repo =
592+
insert(:github_repo, github_id: @payload["repository"]["id"])
593+
594+
insert(:task_list, project: project, done: true)
595+
insert(:task_list, project: project, inbox: true)
596+
597+
attrs =
598+
@payload["comment"]
599+
|> Adapters.Comment.to_github_comment()
600+
|> Map.put(:github_repo, github_repo)
601+
602+
github_comment = insert(:github_comment, attrs)
603+
# creates a user for each comment, which should never happen normally
604+
insert_pair(:comment, github_comment: github_comment)
605+
606+
assert {:error, :multiple_comment_users_match} ==
607+
@payload |> Sync.issue_comment_event()
608+
end
609+
end
610+
611+
describe "issue_comment_event/1 on comment deleted" do
612+
test "syncs correctly" do
464613
%{"comment" => %{"id" => github_id}} = payload =
465614
load_event_fixture("issue_comment_deleted")
466615

0 commit comments

Comments
 (0)