Skip to content
40 changes: 40 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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: Run unit tests with coverage
run: go test -cover ./...

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.25.1"

# Fail if any files need formatting
- name: Check formatting
run: test -z "$(go fmt ./...)"

# Install and run staticcheck
- name: Install staticcheck
run: go install honnef.co/go/tools/cmd/staticcheck@latest
- name: Lint with staticcheck
run: staticcheck ./...
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[![CI](https://github.com/bentu578/learn-cicd-starter/actions/workflows/ci.yml/badge.svg)](https://github.com/bentu578/learn-cicd-starter/actions/workflows/ci.yml)


# 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).
Expand All @@ -21,3 +24,6 @@ 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!


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

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

func TestGetAPIKey_TableDriven(t *testing.T) {
makeHdr := func(v string) http.Header {
h := http.Header{}
if v != "" {
h.Set("Authorization", v)
}
return h
}

tests := []struct {
name string
header string
wantKey string
wantErr bool
wantNoHdr bool // specifically expect ErrNoAuthHeaderIncluded
}{
{
name: "success",
header: "ApiKey abc123",
wantKey: "abc123",
},
{
name: "missing header",
header: "",
wantErr: true,
wantNoHdr: true,
},
{
name: "wrong scheme",
header: "Bearer abc123",
wantErr: true,
},
{
name: "empty key allowed by implementation",
header: "ApiKey ",
wantKey: "",
},
{
name: "key with spaces returns first token only",
header: "ApiKey too many parts",
wantKey: "too",
},
}

for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
key, err := GetAPIKey(makeHdr(tc.header))

if tc.wantErr {
if err == nil {
t.Fatalf("expected error, got nil (key=%q)", key)
}
if tc.wantNoHdr && !errors.Is(err, ErrNoAuthHeaderIncluded) {
t.Fatalf("expected ErrNoAuthHeaderIncluded, got %v", err)
}
return
}

if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if key != tc.wantKey {
t.Fatalf("want key %q, got %q", tc.wantKey, key)
}
})
}
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func main() {
if _, err := io.Copy(w, f); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}

})

v1Router := chi.NewRouter()
Expand Down