| 1 | package tinygit_test |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/FelineStateMachine/tinyrelay/tinygit" |
| 12 | ) |
| 13 | |
| 14 | func TestStandaloneServerRequiresExplicitValidConfiguration(t *testing.T) { |
| 15 | t.Chdir(t.TempDir()) |
| 16 | owner := signedAnnouncement(t, strings.Repeat("0" , 63)+"1" ).PubKey |
| 17 | for _, tc := range []struct { |
| 18 | name string |
| 19 | cfg tinygit.ServerConfig |
| 20 | }{ |
| 21 | {"missing owner" , tinygit.ServerConfig{DataDir: t.TempDir()}}, |
| 22 | {"malformed owner" , tinygit.ServerConfig{DataDir: t.TempDir(), Owner: "not-a-key" }}, |
| 23 | {"uppercase owner" , tinygit.ServerConfig{DataDir: t.TempDir(), Owner: strings.ToUpper(owner)}}, |
| 24 | {"missing data" , tinygit.ServerConfig{Owner: owner}}, |
| 25 | {"invalid URL" , tinygit.ServerConfig{DataDir: t.TempDir(), Owner: owner, PublicURL: "file:///tmp/git" }}, |
| 26 | } { |
| 27 | t.Run(tc.name, func(t *testing.T) { |
| 28 | s, err := tinygit.OpenServer(context.Background(), tc.cfg) |
| 29 | if err == nil { |
| 30 | s.Close() |
| 31 | t.Fatal("invalid configuration accepted" ) |
| 32 | } |
| 33 | }) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func TestStandaloneServerBoundsEventHTTP(t *testing.T) { |
| 38 | owner := signedAnnouncement(t, strings.Repeat("0" , 63)+"1" ).PubKey |
| 39 | s, err := tinygit.OpenServer(context.Background(), tinygit.ServerConfig{DataDir: filepath.Join(t.TempDir(), "data" ), Owner: owner}) |
| 40 | if err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | defer s.Close() |
| 44 | for _, tc := range []struct { |
| 45 | name, method, path, body string |
| 46 | status int |
| 47 | }{ |
| 48 | {"health" , http.MethodGet, "/healthz" , "" , http.StatusOK}, |
| 49 | {"method" , http.MethodGet, "/events" , "" , http.StatusMethodNotAllowed}, |
| 50 | {"body limit" , http.MethodPost, "/events" , strings.Repeat(" " , (1<<20)+1), http.StatusRequestEntityTooLarge}, |
| 51 | {"unsigned" , http.MethodPost, "/events" , `{}` , http.StatusBadRequest}, |
| 52 | } { |
| 53 | t.Run(tc.name, func(t *testing.T) { |
| 54 | w := httptest.NewRecorder() |
| 55 | s.ServeHTTP(w, httptest.NewRequest(tc.method, tc.path, strings.NewReader(tc.body))) |
| 56 | if w.Code != tc.status { |
| 57 | t.Fatalf("HTTP = %d, want %d: %s" , w.Code, tc.status, w.Body.String()) |
| 58 | } |
| 59 | }) |
| 60 | } |
| 61 | } |