@@ -9,6 +9,7 @@ defmodule CodeCorps.GitHub.SyncTest do
99 alias CodeCorps . {
1010 Comment ,
1111 GitHub.Sync ,
12+ GithubAppInstallation ,
1213 GithubComment ,
1314 GithubIssue ,
1415 GithubPullRequest ,
@@ -20,7 +21,89 @@ defmodule CodeCorps.GitHub.SyncTest do
2021 User
2122 }
2223
23- describe "issue_comment_event/1" do
24+ describe "installation_repositories_event/1 added" do
25+
26+ @ payload load_event_fixture ( "installation_repositories_added" )
27+
28+ test "syncs_correctly when adding" do
29+ % { "installation" => % {
30+ "id" => installation_id
31+ } ,
32+ "repositories_added" => added_repos ,
33+ "sender" => % { "id" => _user_id }
34+ } = @ payload
35+
36+ project = insert ( :project )
37+ user = insert ( :user )
38+
39+ insert ( :github_app_installation , github_id: installation_id , project: project , user: user )
40+
41+ { :ok , _repos } = Sync . installation_repositories_event ( @ payload )
42+
43+ repo_ids = Enum . map ( added_repos , & Map . get ( & 1 , "id" ) )
44+
45+ for repo <- Repo . all ( GithubRepo ) do
46+ assert repo . github_id in repo_ids
47+ end
48+
49+ assert Repo . aggregate ( GithubRepo , :count , :id ) == 2
50+ assert Repo . aggregate ( GithubAppInstallation , :count , :id ) == 1
51+ end
52+
53+ test "can fail when installation not found" do
54+ assert { :error , :unmatched_installation , % { } } == @ payload |> Sync . installation_repositories_event ( )
55+ end
56+
57+ test "fails with validation errors when syncing repos" do
58+ % { "installation" => % {
59+ "id" => installation_id
60+ } ,
61+ "repositories_added" => repos ,
62+ "sender" => % { "id" => _user_id }
63+ } = @ payload
64+
65+ project = insert ( :project )
66+ user = insert ( :user )
67+ insert ( :github_app_installation , github_id: installation_id , project: project , user: user )
68+
69+
70+ corrupt_repos = Enum . map ( repos , & ( Map . put ( & 1 , "name" , "" ) ) )
71+
72+ corrupted_payload = Map . put ( @ payload , "repositories_added" , corrupt_repos )
73+
74+ assert { :error , :validation_error_on_syncing_repos , % { } } == corrupted_payload |> Sync . installation_repositories_event ( )
75+ end
76+ end
77+
78+ describe "installation_repositories_event/1 removed" do
79+ @ payload load_event_fixture ( "installation_repositories_removed" )
80+
81+ test "syncs_correctly when removing" do
82+ % { "installation" => % {
83+ "id" => installation_id
84+ } ,
85+ "repositories_removed" => removed_repos
86+ } = @ payload
87+
88+ project = insert ( :project )
89+ user = insert ( :user )
90+ installation = insert ( :github_app_installation , github_id: installation_id , project: project , user: user )
91+
92+ for repo <- removed_repos do
93+ insert ( :github_repo , github_id: repo [ "id" ] , github_app_installation: installation )
94+ end
95+
96+ assert Repo . aggregate ( GithubRepo , :count , :id ) == 2
97+ assert Repo . aggregate ( GithubAppInstallation , :count , :id ) == 1
98+
99+
100+ { :ok , _repos } = Sync . installation_repositories_event ( @ payload )
101+
102+ assert Repo . aggregate ( GithubRepo , :count , :id ) == 0
103+ end
104+ end
105+
106+ describe "issue_comment_event/1 on comment created for pull request" do
24107 @ payload load_event_fixture ( "issue_comment_created_on_pull_request" )
25108
26109 test "syncs the pull request, issue, comment, and user" do
0 commit comments