Skip to content

Commit 7f4571f

Browse files
zacckbegedin
authored andcommitted
Add tests for GitHub.Sync.installation_repositories_event
- "added" scenario, success outcome - "added" scenario, unmatched installation outcome - "added" scenario, validation error on repos outcome - "removed" scenario, success outcome
1 parent 6d6cc63 commit 7f4571f

File tree

3 files changed

+87
-4
lines changed

3 files changed

+87
-4
lines changed

lib/code_corps/github/sync/sync.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ defmodule CodeCorps.GitHub.Sync do
341341
- For the "added" action
342342
-
343343
344-
[https://developer.github.com/v3/activity/events/types/#issuecommentevent](https://developer.github.com/v3/activity/events/types/#issuecommentevent)
344+
[https://developer.github.com/v3/activity/events/types/#installationrepositoriesevent](https://developer.github.com/v3/activity/events/types/#installationrepositoriesevent)
345345
"""
346346
@spec installation_repositories_event(map) ::
347347
installation_repositories_event_outcome()

priv/repo/structure.sql

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
-- PostgreSQL database dump
33
--
44

5-
-- Dumped from database version 10.1
6-
-- Dumped by pg_dump version 10.1
5+
-- Dumped from database version 10.0
6+
-- Dumped by pg_dump version 10.0
77

88
SET statement_timeout = 0;
99
SET lock_timeout = 0;

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

+84-1
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)