diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000000..588ca2de22 --- /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.22.0" + + - name: Print version + 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.22.0" + + - name: Test for style + run: | + echo "Files needing formatting:" + go fmt ./... + echo "Exit code of formatting test:" + test -z "$(go fmt ./...)" || echo $? diff --git a/README.md b/README.md index c2bec0368b..d1b53ff551 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ +![statusBadge](https://github.com/IntrepidSpiff/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 +22,5 @@ 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! + +Tyler's version of Boot.dev's Notely app. diff --git a/internal/auth/auth_test.go b/internal/auth/auth_test.go new file mode 100644 index 0000000000..5e1931baa5 --- /dev/null +++ b/internal/auth/auth_test.go @@ -0,0 +1,54 @@ +package auth + +import ( + "testing" + "net/http" +) + +func TestGetAPIKey (t *testing.T) { + tests := []struct { + name string + headers http.Header + expectedKey string + expectError bool + }{ + { + name: "Valid API key in header", + headers: http.Header{"Authorization": []string{"ApiKey valid-key-123"}}, + expectedKey: "valid-key-123", + expectError: false, + }, + { + name: "Missing Authorization header", + headers: http.Header{}, + expectedKey: "", + expectError: true, + }, + { + name: "Invalid header", + headers: http.Header{"Authorization": []string{"Api Key valid-key-123"}}, + expectedKey: "", + expectError: true, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + key, err := GetAPIKey(tc.headers) + + // Check if error was expected + if tc.expectError && err == nil { + t.Errorf("Expected error but got none") + } + if !tc.expectError && err != nil { + t.Errorf("Did not expect error but got: %v", err) + } + + // If no error was expected, check the key + if !tc.expectError && key != tc.expectedKey { + t.Errorf("Expected key %q, got %q", tc.expectedKey, key) + } + }) + } +} +