diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000000..089904d738 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,42 @@ +name: ci + +on: + pull_request: + branches: [main] + +jobs: + tests: + name: Tests + runs-on: ubuntu-latest + + steps: + - name: Check out code + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: "1.23.0" + + - name: Run Go Unit Test + run: go test -cover ./... + + Style: + name: Style + runs-on: ubuntu-latest + + steps: + - name: Check out code + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: "1.23.0" + + - name: Check formatting + run: test -z $(go fmt ./...) + + + + \ No newline at end of file diff --git a/README.md b/README.md index c2bec0368b..6dfb87f2c8 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +![my workflow](https://github.com/AchiugoTrust/learn-cicd-starter/actions/workflows/ci.yml/badge.svg) + # learn-cicd-starter (Notely) This repo contains the starter code for the "Notely" application for the "Learn CICD" course on [Boot.dev](https://boot.dev). @@ -21,3 +23,4 @@ go build -o notely && ./notely *This starts the server in non-database mode.* It will serve a simple webpage at `http://localhost:8080`. You do *not* need to set up a database or any interactivity on the webpage yet. Instructions for that will come later in the course! +Trust's version of Boot.dev's Notely app xoxo. diff --git a/internal/auth/auth_test.go b/internal/auth/auth_test.go new file mode 100644 index 0000000000..a098f66c3e --- /dev/null +++ b/internal/auth/auth_test.go @@ -0,0 +1,60 @@ +package auth + +import ( + "net/http" + "testing" + "errors" +) + +func TestGetAPIKey(t *testing.T) { + tests := []struct { + name string + header http.Header + expectedKey string + expectingErr bool + expectedError error + }{ + { + name: "valid header", + header: http.Header{"Authorization": []string{"ApiKey abc123"}}, + expectedKey: "abc123", + expectingErr: false, + }, + { + name: "no header", + header: http.Header{}, + expectedKey: "", + expectingErr: true, + expectedError: ErrNoAuthHeaderIncluded, + }, + { + name: "malformed header", + header: http.Header{"Authorization": []string{"Bearer abc123"}}, + expectedKey: "", + expectingErr: true, + expectedError: errors.New("malformed authorization header"), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + key, err := GetAPIKey(tt.header) + + if tt.expectingErr { + if err == nil { + t.Errorf("expected error but got nil") + } else if err.Error() != tt.expectedError.Error() { + t.Errorf("expected error '%v', got '%v'", tt.expectedError, err) + } + } else { + if err != nil { + t.Errorf("unexpected error: %v", err) + } + if key != tt.expectedKey { + t.Errorf("expected key '%s', got '%s'", tt.expectedKey, key) + } + } + }) + } +} + diff --git a/main.go b/main.go index 19d7366c5f..4971e69cc5 100644 --- a/main.go +++ b/main.go @@ -24,6 +24,7 @@ type apiConfig struct { //go:embed static/* var staticFiles embed.FS + func main() { err := godotenv.Load(".env") if err != nil { @@ -37,6 +38,8 @@ func main() { apiCfg := apiConfig{} + + // https://github.com/libsql/libsql-client-go/#open-a-connection-to-sqld // libsql://[your-database].turso.io?authToken=[your-auth-token] dbURL := os.Getenv("DATABASE_URL") @@ -93,6 +96,8 @@ func main() { Handler: router, } + + log.Printf("Serving on port: %s\n", port) log.Fatal(srv.ListenAndServe()) }