Skip to content

Commit

Permalink
chore: remove dependency from test package (#1456)
Browse files Browse the repository at this point in the history
Remove a dependency the testutils/jimmtest package has on github.com/canonical/jimm/v3/cmd/jimmsrv/service
  • Loading branch information
kian99 authored Nov 21, 2024
1 parent f9b68da commit f51e084
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 46 deletions.
49 changes: 36 additions & 13 deletions cmd/jimmsrv/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
"net/url"
"os"
"testing"
"time"

cofga "github.com/canonical/ofga"
"github.com/coreos/go-oidc/v3/oidc"
qt "github.com/frankban/quicktest"
"github.com/go-macaroon-bakery/macaroon-bakery/v3/bakery"
"github.com/go-macaroon-bakery/macaroon-bakery/v3/bakery/checkers"
Expand All @@ -36,12 +38,33 @@ func TestMain(m *testing.M) {
os.Exit(code)
}

// newTestJimmParams returns a set of JIMM params with sensible defaults
// for tests. A test can override any parameter that it needs.
// Note that newTestJimmParams will create an empty test database.
func newTestJimmParams(t jimmtest.Tester) jimmsvc.Params {
return jimmsvc.Params{
DSN: jimmtest.CreateEmptyDatabase(t),
ControllerUUID: "6acf4fd8-32d6-49ea-b4eb-dcb9d1590c11",
PrivateKey: "ly/dzsI9Nt/4JxUILQeAX79qZ4mygDiuYGqc2ZEiDEc=",
PublicKey: "izcYsQy3TePp6bLjqOo3IRPFvkQd2IKtyODGqC6SdFk=",
OAuthAuthenticatorParams: jimmsvc.OAuthAuthenticatorParams{
IssuerURL: "http://localhost:8082/realms/jimm",
ClientID: "jimm-device",
Scopes: []string{oidc.ScopeOpenID, "profile", "email"},
SessionTokenExpiry: time.Duration(time.Hour),
SessionCookieMaxAge: 60,
JWTSessionKey: jimmtest.JWTTestSecret,
},
DashboardFinalRedirectURL: "dashboard-url",
}
}

func TestDefaultService(t *testing.T) {
c := qt.New(t)

_, _, cofgaParams, err := jimmtest.SetupTestOFGAClient(c.Name())
c.Assert(err, qt.IsNil)
p := jimmtest.NewTestJimmParams(c)
p := newTestJimmParams(c)
p.OpenFGAParams = cofgaParamsToJIMMOpenFGAParams(*cofgaParams)
p.InsecureSecretStorage = true
svc, err := jimmsvc.NewService(context.Background(), p)
Expand All @@ -61,7 +84,7 @@ func TestServiceDoesNotStartWithoutCredentialStore(t *testing.T) {

_, _, cofgaParams, err := jimmtest.SetupTestOFGAClient(c.Name())
c.Assert(err, qt.IsNil)
p := jimmtest.NewTestJimmParams(c)
p := newTestJimmParams(c)
p.OpenFGAParams = cofgaParamsToJIMMOpenFGAParams(*cofgaParams)
_, err = jimmsvc.NewService(context.Background(), p)
c.Assert(err, qt.ErrorMatches, "jimm cannot start without a credential store")
Expand All @@ -73,7 +96,7 @@ func TestAuthenticator(t *testing.T) {
_, _, cofgaParams, err := jimmtest.SetupTestOFGAClient(c.Name())
c.Assert(err, qt.IsNil)

p := jimmtest.NewTestJimmParams(c)
p := newTestJimmParams(c)
p.InsecureSecretStorage = true
p.OpenFGAParams = cofgaParamsToJIMMOpenFGAParams(*cofgaParams)
svc, err := jimmsvc.NewService(context.Background(), p)
Expand Down Expand Up @@ -132,7 +155,7 @@ func TestVault(t *testing.T) {
c.Assert(err, qt.IsNil)

vaultClient, _, roleID, roleSecretID, _ := jimmtest.VaultClient(c)
p := jimmtest.NewTestJimmParams(c)
p := newTestJimmParams(c)
p.VaultAddress = "http://localhost:8200"
p.VaultPath = "/jimm-kv/"
p.VaultRoleID = roleID
Expand Down Expand Up @@ -193,7 +216,7 @@ func TestPostgresSecretStore(t *testing.T) {
_, _, cofgaParams, err := jimmtest.SetupTestOFGAClient(c.Name())
c.Assert(err, qt.IsNil)

p := jimmtest.NewTestJimmParams(c)
p := newTestJimmParams(c)
p.InsecureSecretStorage = true
p.OpenFGAParams = cofgaParamsToJIMMOpenFGAParams(*cofgaParams)
svc, err := jimmsvc.NewService(context.Background(), p)
Expand All @@ -208,7 +231,7 @@ func TestOpenFGA(t *testing.T) {
_, _, cofgaParams, err := jimmtest.SetupTestOFGAClient(c.Name())
c.Assert(err, qt.IsNil)

p := jimmtest.NewTestJimmParams(c)
p := newTestJimmParams(c)
p.InsecureSecretStorage = true
p.ControllerAdmins = []string{"alice", "eve"}
p.OpenFGAParams = cofgaParamsToJIMMOpenFGAParams(*cofgaParams)
Expand Down Expand Up @@ -256,7 +279,7 @@ func TestPublicKey(t *testing.T) {
_, _, cofgaParams, err := jimmtest.SetupTestOFGAClient(c.Name())
c.Assert(err, qt.IsNil)

p := jimmtest.NewTestJimmParams(c)
p := newTestJimmParams(c)
p.OpenFGAParams = cofgaParamsToJIMMOpenFGAParams(*cofgaParams)
p.InsecureSecretStorage = true
svc, err := jimmsvc.NewService(context.Background(), p)
Expand All @@ -280,7 +303,7 @@ func TestRebacAdminApi(t *testing.T) {
_, _, cofgaParams, err := jimmtest.SetupTestOFGAClient(c.Name())
c.Assert(err, qt.IsNil)

p := jimmtest.NewTestJimmParams(c)
p := newTestJimmParams(c)
p.InsecureSecretStorage = true
p.OpenFGAParams = cofgaParamsToJIMMOpenFGAParams(*cofgaParams)

Expand Down Expand Up @@ -359,7 +382,7 @@ func TestThirdPartyCaveatDischarge(t *testing.T) {
ofgaClient, _, cofgaParams, err := jimmtest.SetupTestOFGAClient(c.Name())
c.Assert(err, qt.IsNil)

p := jimmtest.NewTestJimmParams(c)
p := newTestJimmParams(c)
p.OpenFGAParams = cofgaParamsToJIMMOpenFGAParams(*cofgaParams)
p.InsecureSecretStorage = true
svc, err := jimmsvc.NewService(context.Background(), p)
Expand Down Expand Up @@ -427,7 +450,7 @@ func TestDisableOAuthEndpointsWhenDashboardRedirectURLNotSet(t *testing.T) {
_, _, cofgaParams, err := jimmtest.SetupTestOFGAClient(c.Name())
c.Assert(err, qt.IsNil)

p := jimmtest.NewTestJimmParams(c)
p := newTestJimmParams(c)
p.DashboardFinalRedirectURL = ""
p.InsecureSecretStorage = true
p.OpenFGAParams = cofgaParamsToJIMMOpenFGAParams(*cofgaParams)
Expand All @@ -450,7 +473,7 @@ func TestEnableOAuthEndpointsWhenDashboardRedirectURLSet(t *testing.T) {
_, _, cofgaParams, err := jimmtest.SetupTestOFGAClient(c.Name())
c.Assert(err, qt.IsNil)

p := jimmtest.NewTestJimmParams(c)
p := newTestJimmParams(c)
p.DashboardFinalRedirectURL = "some-redirect-url"
p.InsecureSecretStorage = true
p.OpenFGAParams = cofgaParamsToJIMMOpenFGAParams(*cofgaParams)
Expand Down Expand Up @@ -498,7 +521,7 @@ func TestCleanupDoesNotPanic_SessionStoreRelatedCleanups(t *testing.T) {

_, _, cofgaParams, err := jimmtest.SetupTestOFGAClient(c.Name())
c.Assert(err, qt.IsNil)
p := jimmtest.NewTestJimmParams(c)
p := newTestJimmParams(c)
p.OpenFGAParams = cofgaParamsToJIMMOpenFGAParams(*cofgaParams)
p.InsecureSecretStorage = true

Expand All @@ -516,7 +539,7 @@ func TestCORS(t *testing.T) {

_, _, cofgaParams, err := jimmtest.SetupTestOFGAClient(c.Name())
c.Assert(err, qt.IsNil)
p := jimmtest.NewTestJimmParams(c)
p := newTestJimmParams(c)
p.OpenFGAParams = cofgaParamsToJIMMOpenFGAParams(*cofgaParams)
allowedOrigin := "http://my-referrer.com"
p.CorsAllowedOrigins = []string{allowedOrigin}
Expand Down
17 changes: 16 additions & 1 deletion internal/jimmjwx/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"
"time"

"github.com/coreos/go-oidc/v3/oidc"
qt "github.com/frankban/quicktest"
"github.com/google/uuid"
"github.com/lestrrat-go/jwx/v2/jwk"
Expand Down Expand Up @@ -97,7 +98,21 @@ func setupService(ctx context.Context, c *qt.C) (*jimmsvc.Service, *httptest.Ser
_, path, roleID, roleSecretID, ok := jimmtest.VaultClient(c)
c.Assert(ok, qt.IsTrue)

p := jimmtest.NewTestJimmParams(c)
p := jimmsvc.Params{
DSN: jimmtest.CreateEmptyDatabase(c),
ControllerUUID: "6acf4fd8-32d6-49ea-b4eb-dcb9d1590c11",
PrivateKey: "ly/dzsI9Nt/4JxUILQeAX79qZ4mygDiuYGqc2ZEiDEc=",
PublicKey: "izcYsQy3TePp6bLjqOo3IRPFvkQd2IKtyODGqC6SdFk=",
OAuthAuthenticatorParams: jimmsvc.OAuthAuthenticatorParams{
IssuerURL: "http://localhost:8082/realms/jimm",
ClientID: "jimm-device",
Scopes: []string{oidc.ScopeOpenID, "profile", "email"},
SessionTokenExpiry: time.Duration(time.Hour),
SessionCookieMaxAge: 60,
JWTSessionKey: jimmtest.JWTTestSecret,
},
DashboardFinalRedirectURL: "dashboard-url",
}
p.VaultAddress = "http://localhost:8200"
p.VaultAuthPath = "/auth/approle/login"
p.VaultPath = path
Expand Down
17 changes: 16 additions & 1 deletion internal/testutils/cmdtest/jimmsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

cofga "github.com/canonical/ofga"
"github.com/coreos/go-oidc/v3/oidc"
"github.com/juju/juju/api"
"github.com/juju/juju/core/network"
corejujutesting "github.com/juju/juju/juju/testing"
Expand Down Expand Up @@ -64,7 +65,21 @@ func (s *JimmCmdSuite) SetUpTest(c *gc.C) {
s.COFGAClient = cofgaClient
s.COFGAParams = cofgaParams

s.Params = jimmtest.NewTestJimmParams(&jimmtest.GocheckTester{C: c})
s.Params = service.Params{
DSN: jimmtest.CreateEmptyDatabase(&jimmtest.GocheckTester{C: c}),
ControllerUUID: "6acf4fd8-32d6-49ea-b4eb-dcb9d1590c11",
PrivateKey: "ly/dzsI9Nt/4JxUILQeAX79qZ4mygDiuYGqc2ZEiDEc=",
PublicKey: "izcYsQy3TePp6bLjqOo3IRPFvkQd2IKtyODGqC6SdFk=",
OAuthAuthenticatorParams: service.OAuthAuthenticatorParams{
IssuerURL: "http://localhost:8082/realms/jimm",
ClientID: "jimm-device",
Scopes: []string{oidc.ScopeOpenID, "profile", "email"},
SessionTokenExpiry: time.Duration(time.Hour),
SessionCookieMaxAge: 60,
JWTSessionKey: jimmtest.JWTTestSecret,
},
DashboardFinalRedirectURL: "dashboard-url",
}
dsn, err := url.Parse(s.Params.DSN)
c.Assert(err, gc.Equals, nil)
s.databaseName = strings.ReplaceAll(dsn.Path, "/", "")
Expand Down
31 changes: 0 additions & 31 deletions internal/testutils/jimmtest/jimm.go

This file was deleted.

0 comments on commit f51e084

Please sign in to comment.