diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000000..3437252dfc4 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,22 @@ +name: ci + +on: + pull_request: + branches: [main] + +jobs: + tests: + name: Tests + runs-on: ubuntu-latest + + steps: + - name: Check out code + uses: actions/checkout@v6 + + - name: Set up Go + uses: actions/setup-go@v6 + with: + go-version: "1.26.0" + + - name: Run get_api_key test + run: go test -cover ./... \ No newline at end of file diff --git a/README.md b/README.md index c2bec0368b7..a40e750a073 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,9 @@ # learn-cicd-starter (Notely) +Tests status: + +![Tests status](https://github.com/przem4l/bootdev-cicd/actions/workflows/ci.yml/badge.svg) + This repo contains the starter code for the "Notely" application for the "Learn CICD" course on [Boot.dev](https://boot.dev). ## Local Development @@ -21,3 +25,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! + +przem4l's version of Boot.dev's Notely app. \ No newline at end of file diff --git a/internal/auth/get_api_key_test.go b/internal/auth/get_api_key_test.go new file mode 100644 index 00000000000..32093418320 --- /dev/null +++ b/internal/auth/get_api_key_test.go @@ -0,0 +1,52 @@ +package auth + +import ( + "errors" + "net/http" + "testing" +) + +func TestGetAPIKey(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + headers http.Header + want string + wantErr error + }{ + { + name: "returns api key from authorization header", + headers: http.Header{"Authorization": []string{"ApiKey abc123"}}, + want: "abc123", + }, + { + name: "fails when authorization header is missing", + headers: http.Header{}, + wantErr: ErrNoAuthHeaderIncluded, + }, + } + + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + got, err := GetAPIKey(tt.headers) + if tt.wantErr != nil { + if !errors.Is(err, tt.wantErr) { + t.Fatalf("GetAPIKey() error = %v, want %v", err, tt.wantErr) + } + return + } + + if err != nil { + t.Fatalf("GetAPIKey() unexpected error = %v", err) + } + + if got != tt.want { + t.Fatalf("GetAPIKey() = %q, want %q", got, tt.want) + } + }) + } +}