Skip to content

Commit 744e2ec

Browse files
zacckbegedin
authored andcommitted
Test Github.Sync.installation/1 event (#1379)
Add tests for different outcomes of installation event handling - success - installation validation errors - multiple unprocessed installations - api error Other described cases are not reachable in any practical way.
1 parent 6463cd6 commit 744e2ec

1 file changed

Lines changed: 42 additions & 1 deletion

File tree

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,49 @@ defmodule CodeCorps.GitHub.SyncTest do
2424

2525
alias Ecto.Changeset
2626

27-
describe "installation_repositories_event/1 added" do
2827

28+
# Some clauses defined seem difficult or impossible to reach so their tests were omitted
29+
# - {:error, :validation_error_on_syncing_installation, Changeset.t()}
30+
# - {:error, :validation_error_on_marking_installation_processed, Changeset.t()}
31+
# - {:error, :unexpected_transaction_outcome, any}
32+
# However, if these clauses can be caused by some updates upstream we should cover them with tests
33+
34+
describe "installation_event" do
35+
36+
@payload load_event_fixture("installation_created")
37+
38+
test "syncs_correctly_with valid data" do
39+
%{"installation" => %{"id" => installation_id}} = @payload
40+
41+
assert Repo.aggregate(GithubAppInstallation, :count, :id) == 0
42+
43+
{:ok, installation} = Sync.installation_event(@payload)
44+
45+
46+
assert Repo.aggregate(GithubAppInstallation, :count, :id) == 1
47+
assert installation.github_id == installation_id
48+
end
49+
50+
test "fails if multiple installations are unprocessed" do
51+
user = insert(:user, github_id: @payload["sender"]["id"])
52+
project = insert(:project)
53+
attrs = %{project: project, user: user, sender_github_id: user.id, github_id: nil}
54+
insert(:github_app_installation, attrs)
55+
insert(:github_app_installation, attrs)
56+
57+
{:error, :multiple_unprocessed_installations_found} = Sync.installation_event(@payload)
58+
end
59+
60+
test "fails on syncing api error" do
61+
with_mock_api(CodeCorps.GitHub.FailureAPI) do
62+
assert {:error, :github_api_error_on_syncing_repos, _error} = Sync.installation_event(@payload)
63+
end
64+
end
65+
end
66+
67+
68+
69+
describe "installation_repositories_event/1 added" do
2970
@payload load_event_fixture("installation_repositories_added")
3071

3172
test "syncs_correctly when adding" do

0 commit comments

Comments
 (0)