diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index b95cb3d..11401a1 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -32,7 +32,9 @@ jobs: key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} restore-keys: | ${{ runner.os }}-go- + - name: Install musl tools + run: sudo apt install unzip musl-tools -y - name: Run lint run: make lint - name: Run tests - run: make test + run: make test-ci diff --git a/Makefile b/Makefile index 468abf8..4dcac07 100644 --- a/Makefile +++ b/Makefile @@ -61,8 +61,8 @@ lint: golangci-lint ## Run golangci-lint. clean: ## Runs go clean go clean -i -test: ## Test the project - go test ./... -coverprofile cover.out +test-ci: ## Test the project + CC=musl-gcc CGO_LDFLAGS="-lm" CGO_ENABLED=1 go test ./... -coverprofile cover.out .PHONY: prime-test-cluster prime-test-cluster: mkcert diff --git a/pkg/bitwarden/bitwarden.go b/pkg/bitwarden/bitwarden.go index bfdbb60..3f4c726 100644 --- a/pkg/bitwarden/bitwarden.go +++ b/pkg/bitwarden/bitwarden.go @@ -66,6 +66,9 @@ func setOrDefault(v, def string) string { return def } +// newBitwardenClientFn is used to overwrite how to initialize the bitwarden client. +var newBitwardenClientFn = sdk.NewBitwardenClient + // Login creates a session for further Bitwarden requests. // Note: I don't like returning the interface, but that's what // the client returns. @@ -77,7 +80,7 @@ func Login(req *LoginRequest) (sdk.BitwardenClientInterface, error) { // Client is closed in the calling handlers. slog.Debug("constructed client with api and identity url", "api", apiURL, "identityUrl", identityURL, "statePath", statePath) - bitwardenClient, err := sdk.NewBitwardenClient(&apiURL, &identityURL) + bitwardenClient, err := newBitwardenClientFn(&apiURL, &identityURL) if err != nil { return nil, fmt.Errorf("failed to create client: %w", err) } diff --git a/pkg/bitwarden/bitwarden_test.go b/pkg/bitwarden/bitwarden_test.go index eea98b8..308d2d2 100644 --- a/pkg/bitwarden/bitwarden_test.go +++ b/pkg/bitwarden/bitwarden_test.go @@ -16,8 +16,6 @@ package bitwarden import ( "bytes" - "encoding/json" - "fmt" "io" "net/http" "net/http/httptest" @@ -34,29 +32,46 @@ import ( // regenerated by a valid bitwarden secret manager machine account. const testToken = "0.0f0a8a7e-d737-498d-be6a-b1930064c31c.Rjqj7Vt7ZcDADtpbuzgx0hqbNJFPho:RZbls0Ka2gwIVKaLvSG3eA==" -// TODO: I need to figure out what the command requires as a response. It seems like it doesn't like -// whatever I throw at it. -func XTestWardenWithToken(t *testing.T) { - // this is the bitwarden server mock - success := sdk.APIKeyLoginResponse{ - Authenticated: true, - } - marshal, _ := json.Marshal(success) - message := json.RawMessage(marshal) - wrapper := struct { - Success bool `json:"success"` - ErrorMessage *string `json:"errorMessage"` - Data *json.RawMessage `json:"data"` - }{ - Success: true, - ErrorMessage: nil, - Data: &message, +type testClient struct{} + +var _ sdk.BitwardenClientInterface = &testClient{} + +type testSecrets struct { + sdk.SecretsInterface +} + +var _ sdk.SecretsInterface = &testSecrets{} + +func (t *testClient) AccessTokenLogin(accessToken string, statePath *string) error { + return nil +} + +func (t *testClient) Projects() sdk.ProjectsInterface { + return &sdk.Projects{} +} + +func (t *testClient) Secrets() sdk.SecretsInterface { + return nil +} + +func (t *testClient) Close() {} + +var testBitwardenClient = func(testClient *testClient) func(apiURL, identityURL *string) (sdk.BitwardenClientInterface, error) { + return func(apiURL, identityURL *string) (sdk.BitwardenClientInterface, error) { + return testClient, nil } +} + +func TestWardenWithToken(t *testing.T) { + prevBitwardenClient := newBitwardenClientFn + mockClient := &testClient{} + newBitwardenClientFn = testBitwardenClient(mockClient) + defer func() { + newBitwardenClientFn = prevBitwardenClient + }() bitwardenServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - wr, _ := json.Marshal(wrapper) - w.Write(wr) - w.WriteHeader(http.StatusAccepted) + w.WriteHeader(http.StatusOK) })) defer bitwardenServer.Close() @@ -82,7 +97,6 @@ func XTestWardenWithToken(t *testing.T) { defer resp.Body.Close() content, err := io.ReadAll(resp.Body) require.NoError(t, err) - fmt.Println(string(content)) - + assert.Equal(t, "test", string(content)) assert.Equal(t, http.StatusOK, resp.StatusCode) } diff --git a/pkg/server/server.go b/pkg/server/server.go index 0124e86..6608907 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -57,10 +57,10 @@ func (s *Server) Run(_ context.Context) error { r := chi.NewRouter() r.Use(middleware.Logger) r.Use(middleware.Recoverer) - r.Get("/ready", func(w http.ResponseWriter, r *http.Request) { + r.Get("/ready", func(w http.ResponseWriter, _ *http.Request) { _, _ = w.Write([]byte("ready")) }) - r.Get("/live", func(w http.ResponseWriter, r *http.Request) { + r.Get("/live", func(w http.ResponseWriter, _ *http.Request) { _, _ = w.Write([]byte("live")) })