Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
simonprev committed Apr 26, 2019
1 parent 15ba320 commit bc67c79
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 7 deletions.
8 changes: 4 additions & 4 deletions lib/hook/broadcaster.ex
Expand Up @@ -6,17 +6,17 @@ defmodule Accent.Hook.Broadcaster do
]

@callback notify(Accent.Hook.Context.t()) :: no_return()
@callback external_document_update(Accent.Hook.Context.t()) :: no_return()
@callback external_document_update(:github, Accent.Hook.Context.t()) :: no_return()

@notify_timeout 10_000
@timeout 10_000

def notify(context = %Accent.Hook.Context{}) do
for producer <- @notifiers do
GenStage.call(producer, {:notify, context}, @notify_timeout)
GenStage.call(producer, {:notify, context}, @timeout)
end
end

def external_document_update(:github, context = %Accent.Hook.Context{}) do
GenStage.call(Accent.Hook.Producers.GitHub, {:external_document_update, context}, :infinity)
GenStage.call(Accent.Hook.Producers.GitHub, {:external_document_update, context}, @timeout)
end
end
2 changes: 1 addition & 1 deletion lib/hook/consumers/github.ex
Expand Up @@ -20,7 +20,7 @@ defmodule Accent.Hook.Consumers.GitHub do
{:noreply, [], state}
end

defp handle_event(%Context{user: user, project: project, event: "push", payload: payload}) do
defp handle_event(%Context{user: user, project: project, payload: payload}) do
payload[:ref]
|> ref_to_version(payload[:default_ref], project)
|> sync_and_add_translations(project, user, payload)
Expand Down
16 changes: 15 additions & 1 deletion lib/web/controllers/hook/github_controller.ex
Expand Up @@ -8,14 +8,14 @@ defmodule Accent.Hook.GitHubController do
alias Accent.{Integration, Project, Repo}
alias Accent.Scopes.Integration, as: IntegrationScope

plug(:filter_event_type)
plug(Plug.Assign, canary_action: :hook_update)
plug(:load_and_authorize_resource, model: Project, id_name: "project_id")
plug(:assign_payload)
plug(:update)

def update(conn, _) do
Accent.Hook.external_document_update(:github, %HookContext{
event: "push",
payload: conn.assigns[:payload],
project: conn.assigns[:project],
user: conn.assigns[:current_user]
Expand Down Expand Up @@ -50,4 +50,18 @@ defmodule Accent.Hook.GitHubController do
|> first()
|> Repo.one()
end

defp filter_event_type(conn, _) do
conn
|> get_req_header("x-github-event")
|> case do
["push"] ->
conn

_ ->
conn
|> send_resp(:not_implemented, "")
|> halt()
end
end
end
2 changes: 1 addition & 1 deletion lib/web/router.ex
Expand Up @@ -46,7 +46,7 @@ defmodule Accent.Router do
get("/export", ExportController, [])
get("/jipt-export", ExportJIPTController, [])

post("/hooks/github", Hook.GitHubController, [])
post("/hooks/github", Hook.GitHubController, [], as: :hooks_github)
end

scope "/", Accent do
Expand Down
111 changes: 111 additions & 0 deletions test/web/controllers/hook/github_controller_test.exs
@@ -0,0 +1,111 @@
defmodule AccentTest.Hook.GitHubController do
use Accent.ConnCase

import Mox
setup :verify_on_exit!

alias Accent.Hook.Context, as: HookContext

alias Accent.{
AccessToken,
Collaborator,
Integration,
Project,
Repo,
User
}

@user %User{email: "test@test.com"}

setup do
user = Repo.insert!(@user)
access_token = %AccessToken{user_id: user.id, token: "test-token"} |> Repo.insert!()
project = %Project{main_color: "#f00", name: "My project"} |> Repo.insert!()
%Collaborator{project_id: project.id, user_id: user.id, role: "bot"} |> Repo.insert!()

{:ok, [access_token: access_token, user: user, project: project]}
end

test "broadcast event on push", %{user: user, access_token: access_token, conn: conn, project: project} do
params = %{
"ref" => "refs/heads/master",
"repository" => %{
"full_name" => "accent/test-repo"
}
}

data = %{default_ref: "master", repository: "accent/test-repo", token: "1234"}
Repo.insert!(%Integration{project_id: project.id, user_id: user.id, service: "github", data: data})

payload = %{
default_ref: "master",
ref: "refs/heads/master",
repository: "accent/test-repo",
token: "1234"
}

Accent.Hook.BroadcasterMock
|> expect(:external_document_update, fn :github, %HookContext{payload: ^payload} -> :ok end)

response =
conn
|> put_req_header("x-github-event", "push")
|> post(hooks_github_path(conn, []) <> "?authorization=#{access_token.token}&project_id=#{project.id}", params)

assert response.status == 204
end

test "don’t broadcast event on other event", %{user: user, access_token: access_token, conn: conn, project: project} do
params = %{
"ref" => "refs/heads/master",
"repository" => %{
"full_name" => "accent/test-repo"
}
}

data = %{default_ref: "master", repository: "accent/test-repo", token: "1234"}
Repo.insert!(%Integration{project_id: project.id, user_id: user.id, service: "github", data: data})

response =
conn
|> put_req_header("x-github-event", "pull_request_comment")
|> post(hooks_github_path(conn, []) <> "?authorization=#{access_token.token}&project_id=#{project.id}", params)

assert response.status == 501
end

test "don’t broadcast event on non existing integration", %{access_token: access_token, conn: conn, project: project} do
params = %{
"ref" => "refs/heads/master",
"repository" => %{
"full_name" => "accent/test-repo"
}
}

response =
conn
|> put_req_header("x-github-event", "push")
|> post(hooks_github_path(conn, []) <> "?authorization=#{access_token.token}&project_id=#{project.id}", params)

assert response.status == 204
end

test "don’t broadcast event on non matching integration", %{user: user, access_token: access_token, conn: conn, project: project} do
params = %{
"ref" => "refs/heads/master",
"repository" => %{
"full_name" => "accent/test-repo"
}
}

data = %{default_ref: "master", repository: "accent/other-repo", token: "1234"}
Repo.insert!(%Integration{project_id: project.id, user_id: user.id, service: "github", data: data})

response =
conn
|> put_req_header("x-github-event", "push")
|> post(hooks_github_path(conn, []) <> "?authorization=#{access_token.token}&project_id=#{project.id}", params)

assert response.status == 204
end
end

0 comments on commit bc67c79

Please sign in to comment.