Skip to content

Commit

Permalink
added a working unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Gergely Brautigam <182850+Skarlso@users.noreply.github.com>
  • Loading branch information
Skarlso committed Jul 1, 2024
1 parent 4555caa commit 5b22d93
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ clean: ## Runs go clean
go clean -i

test: ## Test the project
go test ./... -coverprofile cover.out
CGO_ENABLED=1 go test ./... -coverprofile cover.out

.PHONY: prime-test-cluster
prime-test-cluster: mkcert
Expand Down
5 changes: 4 additions & 1 deletion pkg/bitwarden/bitwarden.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}
Expand Down
62 changes: 38 additions & 24 deletions pkg/bitwarden/bitwarden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ package bitwarden

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
Expand All @@ -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()

Expand All @@ -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)
}
4 changes: 2 additions & 2 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
})

Expand Down

0 comments on commit 5b22d93

Please # to comment.