| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/FelineStateMachine/tinyrelay/protocol/auth" |
| 10 | ) |
| 11 | |
| 12 | func TestGitTokenMintsAReusableRepositoryRootProof(t *testing.T) { |
| 13 | secret := strings.Repeat("7" , 64) |
| 14 | t.Setenv("TINY_AGENT_KEY" , secret) |
| 15 | var out bytes.Buffer |
| 16 | if err := gitToken([]string{"--repo" , "https://relay.example/npub1owner/notes.git/info/refs?service=git-upload-pack" , "--format" , "value" }, &out); err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | token := strings.TrimSpace(out.String()) |
| 20 | validator := auth.NewValidator(time.Now) |
| 21 | for _, request := range []string{"https://relay.example/npub1owner/notes.git/git-upload-pack" , "https://relay.example/npub1owner/notes.git/git-receive-pack" } { |
| 22 | if _, err := validator.VerifyGRASP08("Nostr " +token, request); err != nil { |
| 23 | t.Fatalf("%s: %v" , request, err) |
| 24 | } |
| 25 | } |
| 26 | out.Reset() |
| 27 | if err := gitToken([]string{"--repo" , "https://relay.example/npub1owner/notes.git" , "--format" , "git" }, &out); err != nil || !strings.HasPrefix(out.String(), "http.extraHeader=Authorization: Nostr " ) { |
| 28 | t.Fatalf("git format: %v %q" , err, out.String()) |
| 29 | } |
| 30 | t.Setenv("TINY_AGENT_KEY" , "nsec1vl029mgpspedva04g90vltkh6fvh240zqtv9k0t9af8935ke9laqsnlfe5" ) |
| 31 | out.Reset() |
| 32 | if err := gitToken([]string{"--repo" , "https://relay.example/npub1owner/notes.git" , "--format" , "value" }, &out); err != nil { |
| 33 | t.Fatalf("nsec key: %v" , err) |
| 34 | } |
| 35 | if err := gitToken([]string{"--repo" , "https://relay.example/not-a-repo" }, &out); err == nil { |
| 36 | t.Fatal("non-repository URL accepted" ) |
| 37 | } |
| 38 | t.Setenv("TINY_AGENT_KEY" , "" ) |
| 39 | if err := gitToken([]string{"--repo" , "https://relay.example/npub1owner/notes.git" }, &out); err == nil { |
| 40 | t.Fatal("missing key accepted" ) |
| 41 | } |
| 42 | } |