diff --git a/internal/jimm/model_test.go b/internal/jimm/model_test.go index f03a7ba97..57f71eb80 100644 --- a/internal/jimm/model_test.go +++ b/internal/jimm/model_test.go @@ -1290,12 +1290,6 @@ models: access: admin ` -// TestGetAllModelSummariesForUser ensures that multiple controller calls are made -// and the results are merged. Due to some mocking constraints it may appear unclear, but -// please result the test result for details as to why this is working. -// -// This is essentially a best effort test and in future, with a refactor of how we -// mock the juju client API, we should provide the ability to have multiple controllers. func TestGetAllModelSummariesForUser(t *testing.T) { c := qt.New(t) ctx := context.Background() @@ -1309,27 +1303,55 @@ func TestGetAllModelSummariesForUser(t *testing.T) { Database: db.Database{ DB: jimmtest.PostgresDB(c, nil), }, - Dialer: &jimmtest.Dialer{ - API: &jimmtest.API{ - ListModelSummaries_: func(ctx context.Context, args *jujuparams.ModelSummariesRequest, out *jujuparams.ModelSummaryResults) error { - out.Results = []jujuparams.ModelSummaryResult{ - // This one should be filtered out - { - Result: &jujuparams.ModelSummary{ - Name: "a controller model summary", - IsController: true, + Dialer: &jimmtest.DialerMap{ + "controller-1": &jimmtest.Dialer{ + API: &jimmtest.API{ + ListModelSummaries_: func(ctx context.Context, args *jujuparams.ModelSummariesRequest, out *jujuparams.ModelSummaryResults) error { + out.Results = []jujuparams.ModelSummaryResult{ + // This one should be filtered out + { + Result: &jujuparams.ModelSummary{ + Name: "a controller model summary", + IsController: true, + }, }, - }, - // As we can't return two calls based on "some" controller id, - // we're just returning this model summary for this model - { - Result: &jujuparams.ModelSummary{ - Name: "a model summary that exists", - UUID: "00000002-0000-0000-0000-000000000001", + // As we can't return two calls based on "some" controller id, + // we're just returning this model summary for this model + { + Result: &jujuparams.ModelSummary{ + Name: "a model summary that exists", + UUID: "00000002-0000-0000-0000-000000000001", + ControllerUUID: "00000001-0000-0000-0000-000000000001", + }, }, - }, - } - return nil + } + return nil + }, + }, + }, + "controller-2": &jimmtest.Dialer{ + API: &jimmtest.API{ + ListModelSummaries_: func(ctx context.Context, args *jujuparams.ModelSummariesRequest, out *jujuparams.ModelSummaryResults) error { + out.Results = []jujuparams.ModelSummaryResult{ + // This one should be filtered out + { + Result: &jujuparams.ModelSummary{ + Name: "a controller model summary", + IsController: true, + }, + }, + // As we can't return two calls based on "some" controller id, + // we're just returning this model summary for this model + { + Result: &jujuparams.ModelSummary{ + Name: "a model summary that exists", + UUID: "00000003-0000-0000-0000-000000000002", + ControllerUUID: "00000002-0000-0000-0000-000000000002", + }, + }, + } + return nil + }, }, }, }, @@ -1346,24 +1368,26 @@ func TestGetAllModelSummariesForUser(t *testing.T) { user := openfga.NewUser(dbUser, client) - // The result will be two models of same UUID but it still proves our logic is sound. - // - // Because 2 controllers where alice has "some model", 2 api calls. - // Because we cannot mock response per controller, same model result (controller model and normal model) - // Because we filter controller model, result goes from 4 -> 2 - // And finally, because alice has admin access, it is set on the models - // - // Essentially this still proves we're calling two controllers, filtering controller models - // and merging the result - albeit not so clearly. - + // Test that from two controllers, each with a single model, we get a combined + // summary res, err := j.GetAllModelSummariesForUser(ctx, user) c.Assert(err, qt.IsNil) c.Assert(res.Results, qt.HasLen, 2) // 4 with controller models, but they're filtered - r0 := res.Results[0].Result.UserAccess - r1 := res.Results[1].Result.UserAccess - c.Assert(r0, qt.Equals, jujuparams.UserAccessPermission("admin")) - c.Assert(r1, qt.Equals, jujuparams.UserAccessPermission("admin")) + sort.Slice(res.Results, func(i, j int) bool { + return res.Results[i].Result.ControllerUUID < res.Results[j].Result.ControllerUUID + }) + + res1 := res.Results[0].Result + res2 := res.Results[1].Result + + c.Assert(res1.UserAccess, qt.Equals, jujuparams.UserAccessPermission("admin")) + c.Assert(res1.UUID, qt.Equals, "00000002-0000-0000-0000-000000000001") + c.Assert(res1.ControllerUUID, qt.Equals, "00000001-0000-0000-0000-000000000001") + + c.Assert(res2.UserAccess, qt.Equals, jujuparams.UserAccessPermission("admin")) + c.Assert(res2.UUID, qt.Equals, "00000003-0000-0000-0000-000000000002") + c.Assert(res2.ControllerUUID, qt.Equals, "00000002-0000-0000-0000-000000000002") } func TestModelInfo(t *testing.T) {