From e764423f967dfef52d5817066aad178ce0623a8b Mon Sep 17 00:00:00 2001 From: Darius Rapalis Date: Sun, 16 Mar 2025 13:06:17 +0200 Subject: [PATCH 1/7] Readme update --- .gitignore | 2 ++ README.md | 2 ++ 2 files changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index 2092f54..acbaff2 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ out .env learn-cicd-starter notely + +.idea \ No newline at end of file diff --git a/README.md b/README.md index c2bec03..22d5051 100644 --- a/README.md +++ b/README.md @@ -21,3 +21,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! + +RDarius' version of Boot.dev's Notely app \ No newline at end of file From 278c04859679cbb200602527f08659571f73c3f3 Mon Sep 17 00:00:00 2001 From: Darius Rapalis Date: Sun, 16 Mar 2025 13:09:29 +0200 Subject: [PATCH 2/7] CI --- .github/workflows/ci.yml | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..f48b49e --- /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@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: "1.23.0" + + - name: Force Failure + run: (exit 1) \ No newline at end of file From 0f606fdb9b578ad6edf216c46311e442768b39bd Mon Sep 17 00:00:00 2001 From: Darius Rapalis Date: Sun, 16 Mar 2025 13:10:41 +0200 Subject: [PATCH 3/7] CI update --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f48b49e..c5c6ab5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,4 +19,4 @@ jobs: go-version: "1.23.0" - name: Force Failure - run: (exit 1) \ No newline at end of file + run: go version \ No newline at end of file From a8b6151797d68f6c550cdab4c5647216260b0036 Mon Sep 17 00:00:00 2001 From: Darius Rapalis Date: Sun, 16 Mar 2025 13:17:10 +0200 Subject: [PATCH 4/7] tests --- .github/workflows/ci.yml | 2 +- internal/auth/auth_test.go | 71 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 internal/auth/auth_test.go diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c5c6ab5..3e644c6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,4 +19,4 @@ jobs: go-version: "1.23.0" - name: Force Failure - run: go version \ No newline at end of file + run: go test ./... \ No newline at end of file diff --git a/internal/auth/auth_test.go b/internal/auth/auth_test.go new file mode 100644 index 0000000..afd6179 --- /dev/null +++ b/internal/auth/auth_test.go @@ -0,0 +1,71 @@ +package auth + +import ( + "errors" + "net/http" + "testing" +) + +func TestGetAPIKey_ValidHeader(t *testing.T) { + headers := http.Header{} + headers.Set("Authorization", "ApiKey valid-api-key-123") + + apiKey, err := GetAPIKey(headers) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + if apiKey != "valid-api-key-123" { + t.Fatalf("expected API key to be 'valid-api-key-123', got '%s'", apiKey) + } +} + +func TestGetAPIKey_MissingHeader(t *testing.T) { + headers := http.Header{} + + _, err := GetAPIKey(headers) + if err == nil { + t.Fatal("expected an error, got nil") + } + if !errors.Is(err, ErrNoAuthHeaderIncluded) { + t.Fatalf("expected error to be 'ErrNoAuthHeaderIncluded', got '%v'", err) + } +} + +func TestGetAPIKey_MalformedHeader_MissingPrefix(t *testing.T) { + headers := http.Header{} + headers.Set("Authorization", "Bearer invalid-api-key") // Invalid prefix, should be ApiKey + + _, err := GetAPIKey(headers) + if err == nil { + t.Fatal("expected an error, got nil") + } + if err.Error() != "malformed authorization header" { + t.Fatalf("expected error to be 'malformed authorization header', got '%v'", err) + } +} + +func TestGetAPIKey_MalformedHeader_MissingKey(t *testing.T) { + headers := http.Header{} + headers.Set("Authorization", "ApiKey") // Missing key part + + _, err := GetAPIKey(headers) + if err == nil { + t.Fatal("expected an error, got nil") + } + if err.Error() != "malformed authorization header" { + t.Fatalf("expected error to be 'malformed authorization header', got '%v'", err) + } +} + +func TestGetAPIKey_EmptyHeader(t *testing.T) { + headers := http.Header{} + headers.Set("Authorization", "") // Empty header value + + _, err := GetAPIKey(headers) + if err == nil { + t.Fatal("expected an error, got nil") + } + if !errors.Is(err, ErrNoAuthHeaderIncluded) { + t.Fatalf("expected error to be 'ErrNoAuthHeaderIncluded', got '%v'", err) + } +} From 9d034c2f496b4377878b415dc8d26fd8b68028a7 Mon Sep 17 00:00:00 2001 From: Darius Rapalis Date: Sun, 16 Mar 2025 13:18:26 +0200 Subject: [PATCH 5/7] rename workflow --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3e644c6..4810482 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,5 +18,5 @@ jobs: with: go-version: "1.23.0" - - name: Force Failure + - name: Tests run: go test ./... \ No newline at end of file From ca1568676b649ef952ea6e4d4b738dcd09fce6cd Mon Sep 17 00:00:00 2001 From: Darius Rapalis Date: Sun, 16 Mar 2025 13:20:13 +0200 Subject: [PATCH 6/7] add code coverage --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4810482..eacc966 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,4 +19,4 @@ jobs: go-version: "1.23.0" - name: Tests - run: go test ./... \ No newline at end of file + run: go test ./... -cover \ No newline at end of file From df082ff7e037a218ba85dbb7248dfd471682f5c2 Mon Sep 17 00:00:00 2001 From: Darius Rapalis Date: Sun, 16 Mar 2025 13:23:02 +0200 Subject: [PATCH 7/7] readme badge --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 22d5051..df840da 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +![tests](https://github.com/rdarius/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).