Skip to content

Commit

Permalink
[ADD] New tests - POST for gitea, gitlab and github well-formed payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
blankoworld committed Aug 15, 2019
1 parent d7e35e2 commit aeab77c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
61 changes: 61 additions & 0 deletions spec/gachette_spec.cr
@@ -1,9 +1,70 @@
require "spec-kemal"
require "../src/gachette.cr"
require "./spec_helper.cr"

describe "Gachette URLs" do
it "GET '/' \"J'ai la gachette facile !\"" do
get "/"
response.status_code.should eq 302
response.body.should eq "" # because we use index.html instead
end

it "POST '/' with well-formed GITEA backend" do
# Trust me, you need configuration!
reset_kemal_config
conf = Kemal.config
conf.secretkey = "3gEsCfjlV2ugRwgpU#w1*WaW*wa4NXgGmpCfkbG3"

Kemal::CLI.new ["-k", "gitea", "-n", "gitea/webhooks", "-c", "ls /"]

json_payload = open_json_file("spec/push_gitea.json")
post("/", HTTP::Headers{
"User-Agent" => "GiteaServer",
"Content-Type" => "application/json",
"X-GitHub-Delivery" => "e905e09b-6b35-46b4-bba5-c6599d21ab33",
"X-GitHub-Event" => "push",
"X-Gitea-Delivery" => "e905e09b-6b35-46b4-bba5-c6599d21ab33",
"X-Gitea-Event" => "push",
"X-Gogs-Delivery" => "e905e09b-6b35-46b4-bba5-c6599d21ab33",
"X-Gogs-Event" => "push ",
}, json_payload)
response.body.should eq("SUCCESS")
end

it "POST '/' with well-formed GITLAB backend" do
# Trust me, you need configuration!
reset_kemal_config
conf = Kemal.config
conf.secretkey = "mot2passe"

Kemal::CLI.new ["-k", "gitlab", "-n", "mike/diaspora", "-c", "ls /"]

json_payload = open_json_file("spec/push_gitlab.json")
post("/", HTTP::Headers{
"Content-Type" => "application/json",
"X-Gitlab-Event" => "Push Hook",
"X-Gitlab-Token" => "mot2passe",
}, json_payload)
response.body.should eq("SUCCESS")
end

it "POST '/' with well-formed GITHUB backend" do
# Trust me, you need configuration!
reset_kemal_config
conf = Kemal.config
conf.secretkey = "quelquechose"

Kemal::CLI.new ["-k", "github", "-n", "blankoworld/gachette", "-c", "ls /"]

json_payload = GITHUB_BODY
post("/", HTTP::Headers{
"content-type" => "application/json",
"Expect" => "",
"User-Agent" => "GitHub-Hookshot/hashsah",
"X-GitHub-Delivery" => "691df212-bdb7-11e9-8119-fa2f1de358a6",
"X-GitHub-Event" => "push",
"X-Hub-Signature" => "sha1=8cfbbbf7a0b7b8322519ac7fbf346ad9a9475236",
}, json_payload)
response.body.should eq("SUCCESS")
end
end
17 changes: 17 additions & 0 deletions spec/spec_helper.cr
@@ -1,13 +1,30 @@
require "spec"

# spec_helper adds method to manipulate data easily in tests

# Just open a file and give its content
def open_json_file(name) : String
file = File.new(name)
content = file.gets_to_end || ""
file.close
content
end

# Reset Kemal new config. properties (from Gachette):
# - kind
# - namespace
# - command
# - scriptfile
# - secretkey
def reset_kemal_config
conf = Kemal.config
conf.kind = "github"
conf.namespace = "blankoworld/gachette"
conf.command = nil
conf.scriptfile = nil
conf.secretkey = ""
end

# Body content from an existing Github webhook
GITHUB_BODY = <<-'TEXT'
{
Expand Down

0 comments on commit aeab77c

Please sign in to comment.