f19b271642
Rebuild the Gitea Codex review bot from the product contract with a Go HTTP service, durable SQL queue, typed Gitea client, isolated runner, and deployment updates. Co-Authored-By: Claude <noreply@anthropic.com>
40 lines
1.3 KiB
Go
40 lines
1.3 KiB
Go
package webhook
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"testing"
|
|
)
|
|
|
|
func TestVerifySignatureUsesRawBodyAndPrefix(t *testing.T) {
|
|
body := []byte(`{"ok":true}`)
|
|
mac := hmac.New(sha256.New, []byte("secret"))
|
|
_, _ = mac.Write(body)
|
|
signature := hex.EncodeToString(mac.Sum(nil))
|
|
if !VerifySignature(body, "secret", "sha256="+signature) {
|
|
t.Fatal("valid signature was rejected")
|
|
}
|
|
if VerifySignature([]byte(`{"ok":false}`), "secret", signature) {
|
|
t.Fatal("changed body was accepted")
|
|
}
|
|
if VerifySignature(body, "wrong", signature) {
|
|
t.Fatal("wrong secret was accepted")
|
|
}
|
|
}
|
|
|
|
func TestParseEvents(t *testing.T) {
|
|
body := []byte(`{"repository":{"full_name":"acme/repo"},"sender":{"username":"alice"},"comment":{"id":11,"body":"@codex review"},"issue":{"number":9,"pull_request":{"url":"x"}},"pull_request":{"head":{"sha":"abc"}}}`)
|
|
event, err := ParseEvent("issue_comment", "delivery-1", body)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if event.Repo != "acme/repo" || event.PRNumber != 9 || event.HeadSHA != "abc" || event.CommentID != 11 {
|
|
t.Fatalf("unexpected event: %#v", event)
|
|
}
|
|
bad := []byte(`{"repository":{"full_name":"acme/repo"},"comment":{"id":1},"issue":{"number":9}}`)
|
|
if _, err := ParseEvent("issue_comment", "", bad); err == nil {
|
|
t.Fatal("non-PR issue comment was accepted")
|
|
}
|
|
}
|