From 9e7f9c26565e3a019b3735043fb2188abaae8b8b Mon Sep 17 00:00:00 2001 From: Yash Pal Singh Date: Mon, 15 Dec 2025 00:43:06 +0530 Subject: [PATCH 01/11] Created new branch and added the name version --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index c2bec0368b7..5f1c6f8ae42 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! + +Yash 1.0.0 From c3332d5c36ae83862fc20cb07bea07bb6909111c Mon Sep 17 00:00:00 2001 From: Yash Pal Singh Date: Mon, 15 Dec 2025 01:00:54 +0530 Subject: [PATCH 02/11] added ci.yml for triggering ci build on pull requests. --- .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 00000000000..a54d8248d54 --- /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.25.1" + + - name: Force Failure + run: (exit 1) \ No newline at end of file From 80f4b02414535b0f9da0fc0271a3f15d13d14a70 Mon Sep 17 00:00:00 2001 From: Yash Pal Singh Date: Mon, 15 Dec 2025 01:06:09 +0530 Subject: [PATCH 03/11] fixed ci.yml by removing force failure block. --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a54d8248d54..12066e72b7f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,5 +18,5 @@ jobs: with: go-version: "1.25.1" - - name: Force Failure - run: (exit 1) \ No newline at end of file + - name: Check Go version + run: go version \ No newline at end of file From a43ca984a4a3c8278d9e5ab0c37ba0948635eec5 Mon Sep 17 00:00:00 2001 From: Yash Pal Singh Date: Mon, 15 Dec 2025 01:40:14 +0530 Subject: [PATCH 04/11] tests created for broken code-ci should catch these and hence gets failed. --- internal/auth/auth_test.go | 104 +++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 internal/auth/auth_test.go diff --git a/internal/auth/auth_test.go b/internal/auth/auth_test.go new file mode 100644 index 00000000000..e9dcbdafef3 --- /dev/null +++ b/internal/auth/auth_test.go @@ -0,0 +1,104 @@ +package auth + +import ( + "net/http" + "testing" +) + +func TestGetAPIKey(t *testing.T) { + // Define the structure for our test cases + tests := []struct { + name string // Name of the test case + headers http.Header // Input headers + expectedKey string // The key we expect to get back + expectedError error // The specific error we expect (for sentinel errors) + errorString string // The error message string (for errors created on the fly) + }{ + { + name: "No Authorization Header", + headers: http.Header{}, // Empty headers + expectedKey: "asdf", + expectedError: ErrNoAuthHeaderIncluded, + }, + { + name: "Malformed Header - Wrong Prefix", + headers: http.Header{ + "Authorization": []string{"Bearer my-token"}, + }, + expectedKey: "", + errorString: "malformed authorization header", + }, + { + name: "Malformed Header - Missing Token", + headers: http.Header{ + "Authorization": []string{"ApiKey"}, // Missing the actual key part + }, + expectedKey: "", + errorString: "malformed authorization header", + }, + { + name: "Valid API Key", + headers: http.Header{ + "Authorization": []string{"ApiKey my-secret-token"}, + }, + expectedKey: "my-secret-token", + expectedError: nil, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotKey, err := GetAPIKey(tt.headers) + + // 1. Check if the returned key matches expectation + if gotKey != tt.expectedKey { + t.Errorf("GetAPIKey() key = %v, want %v", gotKey, tt.expectedKey) + } + + // 2. Check errors + // Scenario A: We expect no error + if tt.expectedError == nil && tt.errorString == "" { + if err != nil { + t.Errorf("GetAPIKey() unexpected error = %v", err) + } + return + } + + // Scenario B: We expect a specific Sentinel Error (ErrNoAuthHeaderIncluded) + if tt.expectedError != nil { + if err != tt.expectedError { + t.Errorf("GetAPIKey() error = %v, want %v", err, tt.expectedError) + } + return + } + + // Scenario C: We expect an error created with errors.New() inside the function + // Since these are new pointers, we compare the error string, not the error object itself + if tt.errorString != "" { + if err == nil || err.Error() != tt.errorString { + t.Errorf("GetAPIKey() error = %v, want error string %v", err, tt.errorString) + } + } + }) + } +} + +// Explanation of the Tests +// "No Authorization Header": Checks if the function correctly returns your exported variable ErrNoAuthHeaderIncluded when the map is empty. + +// "Malformed Header": + +// Wrong Prefix: Checks inputs like Bearer token instead of ApiKey token. + +// Missing Token: Checks inputs that have the prefix but no actual key (length < 2). + +// Note: Because your code generates a new error (errors.New("malformed...")) inside the function, we cannot compare it against a global variable. Instead, the test checks if the error message string matches. + +// "Valid API Key": Checks the happy path where the format is correct (ApiKey ), ensuring it returns the token and nil error. + +// How to run it +// Run this command in your terminal inside the directory: + +// Bash + +// go test -v \ No newline at end of file From d2878d255d9cc2199a5aa4897e356e6baf6af5cb Mon Sep 17 00:00:00 2001 From: Yash Pal Singh Date: Mon, 15 Dec 2025 01:44:30 +0530 Subject: [PATCH 05/11] unit tests fixed. --- internal/auth/auth_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/auth/auth_test.go b/internal/auth/auth_test.go index e9dcbdafef3..e2f365147b5 100644 --- a/internal/auth/auth_test.go +++ b/internal/auth/auth_test.go @@ -17,7 +17,7 @@ func TestGetAPIKey(t *testing.T) { { name: "No Authorization Header", headers: http.Header{}, // Empty headers - expectedKey: "asdf", + expectedKey: "", expectedError: ErrNoAuthHeaderIncluded, }, { From 60f6ae3beac9fa0053dd6977b3e89e36bf282287 Mon Sep 17 00:00:00 2001 From: Yash Pal Singh Date: Mon, 15 Dec 2025 01:49:13 +0530 Subject: [PATCH 06/11] unit tests broken. --- internal/auth/auth_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/auth/auth_test.go b/internal/auth/auth_test.go index e2f365147b5..18d646784de 100644 --- a/internal/auth/auth_test.go +++ b/internal/auth/auth_test.go @@ -17,7 +17,7 @@ func TestGetAPIKey(t *testing.T) { { name: "No Authorization Header", headers: http.Header{}, // Empty headers - expectedKey: "", + expectedKey: "asd", expectedError: ErrNoAuthHeaderIncluded, }, { From c4a2b104a561e0672fd170f3029f01a59b2c63f6 Mon Sep 17 00:00:00 2001 From: Yash Pal Singh Date: Mon, 15 Dec 2025 01:52:51 +0530 Subject: [PATCH 07/11] ci yml not pushed-fixing this. --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 12066e72b7f..10ec7225e10 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,5 +18,5 @@ jobs: with: go-version: "1.25.1" - - name: Check Go version - run: go version \ No newline at end of file + - name: Run tests + run: go test ./... \ No newline at end of file From a1656a59050352a7003b2d0daf5052c795d6902e Mon Sep 17 00:00:00 2001 From: Yash Pal Singh Date: Mon, 15 Dec 2025 01:54:50 +0530 Subject: [PATCH 08/11] ci checked, not fixed unit tests and pushing it. --- internal/auth/auth_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/auth/auth_test.go b/internal/auth/auth_test.go index 18d646784de..e2f365147b5 100644 --- a/internal/auth/auth_test.go +++ b/internal/auth/auth_test.go @@ -17,7 +17,7 @@ func TestGetAPIKey(t *testing.T) { { name: "No Authorization Header", headers: http.Header{}, // Empty headers - expectedKey: "asd", + expectedKey: "", expectedError: ErrNoAuthHeaderIncluded, }, { From eb6c21d90c8081673b8799b8c5e1ae2cedf20a89 Mon Sep 17 00:00:00 2001 From: Yash Pal Singh Date: Mon, 15 Dec 2025 02:14:08 +0530 Subject: [PATCH 09/11] code coverage flag added to ci yml. --- .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 10ec7225e10..16e3f5d7576 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,4 +19,4 @@ jobs: go-version: "1.25.1" - name: Run tests - run: go test ./... \ No newline at end of file + run: go test ./... -cover \ No newline at end of file From 53f684509215508f563d3c275581da0a277a0f06 Mon Sep 17 00:00:00 2001 From: Yash Pal Singh Date: Mon, 15 Dec 2025 02:20:19 +0530 Subject: [PATCH 10/11] adding badge for showing status of tests in readme.md. --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 5f1c6f8ae42..e136250e783 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # learn-cicd-starter (Notely) +![alt text goes here](https://github.com/theguyoncloud/learn-cicd-starter/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 From b28c5c5c742d643313674c659fcbc0ffab02337c Mon Sep 17 00:00:00 2001 From: Yash Pal Singh Date: Mon, 15 Dec 2025 02:23:50 +0530 Subject: [PATCH 11/11] adding badge for showing status of tests in readme.md. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e136250e783..dbcecc682b1 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ -# learn-cicd-starter (Notely) - ![alt text goes here](https://github.com/theguyoncloud/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). ## Local Development