diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000000..63773fcd91 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,34 @@ +name: CI + +on: [push, pull_request] + +jobs: + tests: + name: CI + +on: + pull_request: + +jobs: + tests: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: '1.21' + - name: Run unit tests + run: go test ./... + + style: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: '1.21' + - name: Check formatting + run: test -z $(go fmt ./...) + diff --git a/README.md b/README.md index c2bec0368b..1f7f75acf7 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ +[![CI](https://github.com/Saharyasa/learn-cicd-starter/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/Saharyasa/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). @@ -21,3 +22,4 @@ 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! +Sahar Yasa's version of Boot.dev's Notely app diff --git a/internal/auth/auth_test.go b/internal/auth/auth_test.go new file mode 100644 index 0000000000..c8ca87d322 --- /dev/null +++ b/internal/auth/auth_test.go @@ -0,0 +1,48 @@ +package auth + +import ( + "net/http" + "net/http/httptest" + "testing" +) + +func TestGetAPIKey(t *testing.T) { + tests := []struct { + name string + header string + want string + wantErr bool + }{ + {name: "no header", header: "", wantErr: true}, + {name: "wrong scheme", header: "Bearer abc123", wantErr: true}, + // Function currently returns "" and NO error for an empty ApiKey value + {name: "empty key", header: "ApiKey ", want: "", wantErr: false}, + {name: "valid", header: "ApiKey abc123", want: "abc123", wantErr: false}, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + req := httptest.NewRequest(http.MethodGet, "/", nil) + if tc.header != "" { + req.Header.Set("Authorization", tc.header) + } + + // GetAPIKey takes http.Header + got, err := GetAPIKey(req.Header) + + if tc.wantErr { + if err == nil { + t.Fatalf("expected an error, got key=%q", got) + } + return + } + + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if got != tc.want { + t.Fatalf("want %q, got %q", tc.want, got) + } + }) + } +} diff --git a/trigger.txt b/trigger.txt new file mode 100644 index 0000000000..b022dc85f9 --- /dev/null +++ b/trigger.txt @@ -0,0 +1 @@ +# trigger ci