cli/pkg/unifi/client_test.go
Snider feff6f7a09 Add configuration documentation to README (#304)
* docs: add configuration documentation to README

Added a new 'Configuration' section to README.md as per the
Documentation Audit Report (PR #209).

Included:
- Default configuration file location (~/.core/config.yaml)
- Configuration file format (YAML) with examples
- Layered configuration resolution order
- Environment variable mapping for config overrides (CORE_CONFIG_*)
- Common environment variables (CORE_DAEMON, NO_COLOR, MCP_ADDR, etc.)

* docs: add configuration documentation and fix CI/CD auto-merge

README.md:
- Added comprehensive 'Configuration' section as per audit report #209.
- Documented file format, location, and layered resolution order.
- Provided environment variable mapping rules and common examples.

.github/workflows/auto-merge.yml:
- Replaced broken reusable workflow with a local implementation.
- Added actions/checkout step to provide necessary Git context.
- Fixed 'not a git repository' error by providing explicit repo context
  to the 'gh' CLI via the -R flag.
- Maintained existing bot trust and author association logic.

pkg/io/local/client.go:
- Fixed code formatting to ensure QA checks pass.

* docs: update environment variable description and fix merge conflict

- Refined the description of environment variable mapping to be more accurate,
  clarifying that the prefix is stripped before conversion.
- Resolved merge conflict in .github/workflows/auto-merge.yml.
- Maintained the local auto-merge implementation to ensure Git context
  for the 'gh' CLI.

* docs: configuration documentation, security fixes, and CI improvements

README.md:
- Added comprehensive 'Configuration' section as per audit report #209.
- Documented file format, location, and layered resolution order.
- Provided environment variable mapping rules and common examples.
- Added documentation for UniFi configuration options.

.github/workflows/auto-merge.yml:
- Replaced broken reusable workflow with a local implementation.
- Added actions/checkout step to provide necessary Git context.
- Fixed 'not a git repository' error by providing explicit repo context
  to the 'gh' CLI via the -R flag.

pkg/unifi:
- Fixed security vulnerability (CodeQL) by making TLS verification
  configurable instead of always skipped.
- Added 'unifi.insecure' config key and UNIFI_INSECURE env var.
- Updated New and NewFromConfig signatures to handle insecure flag.

internal/cmd/unifi:
- Added --insecure flag to 'config' command to skip TLS verification.
- Updated all UniFi subcommands to support the new configuration logic.

pkg/io/local/client.go:
- Fixed code formatting to ensure QA checks pass.

* docs: configuration documentation, tests, and CI/CD fixes

README.md:
- Added comprehensive 'Configuration' section as per audit report #209.
- Documented file format, location, and layered resolution order.
- Provided environment variable mapping rules and common examples.
- Documented UniFi configuration options.

pkg/unifi:
- Fixed security vulnerability by making TLS verification configurable.
- Added pkg/unifi/config_test.go and pkg/unifi/client_test.go to provide
  unit test coverage for new and existing logic (satisfying Codecov).

.github/workflows/auto-merge.yml:
- Added actions/checkout@v4 to provide the required Git context for the
  'gh' CLI, fixing 'not a git repository' errors.

pkg/framework/core/core.go:
- Fixed compilation errors in Workspace() and Crypt() methods due to
  upstream changes in MustServiceFor() return signature.
- Added necessary error handling to pkg/workspace/service.go.

These changes ensure that the project documentation is up-to-date and that
the CI/CD pipeline is stable and secure.
2026-02-05 10:56:49 +00:00

50 lines
1.4 KiB
Go

package unifi
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNew(t *testing.T) {
// Mock UniFi controller response for login/initialization
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, `{"meta":{"rc":"ok"}, "data": []}`)
}))
defer ts.Close()
// Test basic client creation
client, err := New(ts.URL, "user", "pass", "", true)
assert.NoError(t, err)
assert.NotNil(t, client)
assert.Equal(t, ts.URL, client.URL())
assert.NotNil(t, client.API())
if client.API().Client != nil && client.API().Client.Transport != nil {
if tr, ok := client.API().Client.Transport.(*http.Transport); ok {
assert.True(t, tr.TLSClientConfig.InsecureSkipVerify)
} else {
t.Errorf("expected *http.Transport, got %T", client.API().Client.Transport)
}
} else {
t.Errorf("client or transport is nil")
}
// Test with insecure false
client, err = New(ts.URL, "user", "pass", "", false)
assert.NoError(t, err)
if tr, ok := client.API().Client.Transport.(*http.Transport); ok {
assert.False(t, tr.TLSClientConfig.InsecureSkipVerify)
}
}
func TestNew_Error(t *testing.T) {
// uf.NewUnifi fails if URL is invalid (e.g. missing scheme)
client, err := New("localhost:8443", "user", "pass", "", false)
assert.Error(t, err)
assert.Nil(t, client)
}