diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000000..324d309ae1 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,38 @@ +name: CI + +on: + pull_request: + branches: [ main ] + +jobs: + 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: Get dependencies + run: go mod download + + - name: Run tests + run: go test ./... + + style: + name: Style + 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: Check formatting + run: test -z "$(go fmt ./...)" diff --git a/README.md b/README.md index c2bec0368b..8d30a9993b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # learn-cicd-starter (Notely) +![Tests](https://github.com/Sashozk/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). @@ -21,3 +22,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! + +Sasho Zikov's version of Boot.dev's Notely app. diff --git a/internal/auth/auth.go b/internal/auth/auth.go index f969aacf63..69f07002c0 100644 --- a/internal/auth/auth.go +++ b/internal/auth/auth.go @@ -2,6 +2,7 @@ package auth import ( "errors" + "fmt" "net/http" "strings" ) @@ -21,3 +22,7 @@ func GetAPIKey(headers http.Header) (string, error) { return splitAuth[1], nil } + +func main() { + fmt.Println("hello world!") +} diff --git a/internal/auth/auth_test.go b/internal/auth/auth_test.go new file mode 100644 index 0000000000..8e573ea807 --- /dev/null +++ b/internal/auth/auth_test.go @@ -0,0 +1,37 @@ +package auth + +import ( + "net/http" + "testing" +) + +func TestGetAPIKey(t *testing.T) { + t.Run("returns key when header is valid", func(t *testing.T) { + h := http.Header{} + h.Set("Authorization", "ApiKey abc123") + got, err := GetAPIKey(h) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if got != "abc123" { + t.Fatalf("expected abc123, got %q", got) + } + }) + + t.Run("returns ErrNoAuthHeaderIncluded when header missing", func(t *testing.T) { + h := http.Header{} + _, err := GetAPIKey(h) + if err != ErrNoAuthHeaderIncluded { + t.Fatalf("expected ErrNoAuthHeaderIncluded, got %v", err) + } + }) + + t.Run("returns error when header malformed", func(t *testing.T) { + h := http.Header{} + h.Set("Authorization", "Bearer abc123") + _, err := GetAPIKey(h) + if err == nil { + t.Fatalf("expected error for malformed header, got nil") + } + }) +}