Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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: print go version
run: |
go test -cover ./...
exit 0
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!

NN's version of Boot.dev's Notely app.
77 changes: 77 additions & 0 deletions internal/auth/auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package auth

import (
"errors"
"net/http"
"testing"
)

func TestGetAPIKey(t *testing.T) {
tests := []struct {
name string
headers http.Header
expectedKey string
expectedError error
}{
{
name: "valid API key with correct format",
headers: http.Header{
"Authorization": []string{"ApiKey my-secret-api-key-12345"},
},
expectedKey: "my-secret-api-key-12345",
expectedError: nil,
},
{
name: "missing Authorization header",
headers: http.Header{},
expectedKey: "",
expectedError: ErrNoAuthHeaderIncluded,
},
{
name: "malformed Authorization header - wrong scheme",
headers: http.Header{
"Authorization": []string{"Bearer my-token"},
},
expectedKey: "",
expectedError: errors.New("malformed authorization header"),
},
{
name: "malformed Authorization header - only ApiKey without value",
headers: http.Header{
"Authorization": []string{"ApiKey"},
},
expectedKey: "",
expectedError: errors.New("malformed authorization header"),
},
{
name: "empty Authorization header value",
headers: http.Header{
"Authorization": []string{""},
},
expectedKey: "",
expectedError: ErrNoAuthHeaderIncluded,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
apiKey, err := GetAPIKey(tt.headers)

// Check the API key
if apiKey != tt.expectedKey {
t.Errorf("GetAPIKey() apiKey = %v, want %v", apiKey, tt.expectedKey)
}

// Check the error
if tt.expectedError != nil && err == nil {
t.Errorf("GetAPIKey() error = nil, want %v", tt.expectedError)
}
if tt.expectedError == nil && err != nil {
t.Errorf("GetAPIKey() error = %v, want nil", err)
}
if tt.expectedError != nil && err != nil && err.Error() != tt.expectedError.Error() {
t.Errorf("GetAPIKey() error = %v, want %v", err, tt.expectedError)
}
})
}
}