From a8b6151797d68f6c550cdab4c5647216260b0036 Mon Sep 17 00:00:00 2001 From: Darius Rapalis Date: Sun, 16 Mar 2025 13:17:10 +0200 Subject: [PATCH] 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) + } +}