This repository has been archived on 2026-07-17. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
gitea-codex/internal/webhook/webhook_test.go
T
Space-Banane f19b271642
ci / test (pull_request) Successful in 1m54s
ci / publish (pull_request) Has been skipped
feat. rebuild service in Go
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>
2026-07-12 21:51:01 +02:00

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")
}
}