Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added a working unit test #7

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
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