Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Commit

Permalink
Added test for userdata service
Browse files Browse the repository at this point in the history
  • Loading branch information
petteja committed Sep 3, 2024
1 parent 53987a0 commit 99a5514
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 19 deletions.
1 change: 1 addition & 0 deletions pkg/database/gensql/dataproducts_v2.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pkg/database/queries/dataproducts_v2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ FROM dataproduct_view dp
LEFT JOIN datasource_bigquery dsrc ON dsrc.dataset_id = dp.ds_id
LEFT JOIN dataset_access_requests dar ON dar.dataset_id = dp.ds_id AND dar.status = 'pending'
WHERE (array_length(@ids::uuid[], 1) IS NULL OR dp_id = ANY (@ids))
AND (array_length(@groups::TEXT[], 1) IS NULL OR dp_group = ANY (@groups));
AND (array_length(@groups::TEXT[], 1) IS NULL OR dp_group = ANY (@groups))
ORDER by dp.dp_group, dp.dp_name;

-- name: GetDataproductWithDatasetsBasic :many
SELECT *
Expand Down
2 changes: 1 addition & 1 deletion test/integration/access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func TestAccess(t *testing.T) {
)

StorageCreateProductAreasAndTeams(t, stores.ProductAreaStorage)
fuel, err := dataproductService.CreateDataproduct(ctx, UserOne, NewDataProductBiofuelProduction())
fuel, err := dataproductService.CreateDataproduct(ctx, UserOne, NewDataProductBiofuelProduction(GroupEmailNada, TeamSeagrassID))
assert.NoError(t, err)

fuelData, err := dataproductService.CreateDataset(ctx, UserOne, NewDatasetBiofuelConsumptionRates(fuel.ID))
Expand Down
36 changes: 24 additions & 12 deletions test/integration/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ var (
TeamReefID = uuid.MustParse("00000000-0000-0000-0000-000000000004")
TeamReefName = "Reef"

GroupNameReef = "reef"
GroupEmailReef = "[email protected]"

UserOneName = "User Userson"
UserOneEmail = "[email protected]"
UserTwoName = "Another Userson"
Expand Down Expand Up @@ -79,6 +82,15 @@ const fakeMetabaseSA = `{
}
`

func StorageCreateNaisConsoleTeamsAndProjects(t *testing.T, storage service.NaisConsoleStorage, teams map[string]string) {
t.Helper()

err := storage.UpdateAllTeamProjects(context.Background(), teams)
if err != nil {
t.Fatalf("creating teams and projects: %v", err)

Check warning on line 90 in test/integration/assets.go

View check run for this annotation

Codecov / codecov/patch

test/integration/assets.go#L90

Added line #L90 was not covered by tests
}
}

func StorageCreateProductAreasAndTeams(t *testing.T, storage service.ProductAreaStorage) {
t.Helper()

Expand Down Expand Up @@ -112,13 +124,13 @@ func StorageCreateProductAreasAndTeams(t *testing.T, storage service.ProductArea
}
}

func NewDataProductBiofuelProduction() service.NewDataproduct {
func NewDataProductBiofuelProduction(group string, teamID uuid.UUID) service.NewDataproduct {
return service.NewDataproduct{
Name: "Biofuel Production",
Description: strToStrPtr("Using seagrass as a feedstock to create renewable biofuels"),
Group: GroupEmailNada,
Group: group,
ProductAreaID: &ProductAreaOceanicID,
TeamID: &TeamSeagrassID,
TeamID: &teamID,
}
}

Expand Down Expand Up @@ -157,33 +169,33 @@ func NewDatasetBiofuelConsumptionRates(dataProductID uuid.UUID) service.NewDatas
}
}

func NewDataProductAquacultureFeed() service.NewDataproduct {
func NewDataProductAquacultureFeed(group string, teamID uuid.UUID) service.NewDataproduct {
return service.NewDataproduct{
Name: "Aquaculture Feed",
Description: strToStrPtr("Producing high-nutrient feed for aquaculture industries from processed seagrass"),
Group: GroupEmailNada,
Group: group,
ProductAreaID: &ProductAreaOceanicID,
TeamID: &TeamSeagrassID,
TeamID: &teamID,
}
}

func NewDataProductReefMonitoring() service.NewDataproduct {
func NewDataProductReefMonitoring(group string, teamID uuid.UUID) service.NewDataproduct {
return service.NewDataproduct{
Name: "Reef Monitoring Equipment",
Description: strToStrPtr("Advanced sensors and monitoring devices for continuous assessment"),
Group: GroupEmailNada,
Group: group,
ProductAreaID: &ProductAreaCostalID,
TeamID: &TeamReefID,
TeamID: &teamID,
}
}

func NewDataProductProtectiveBarriers() service.NewDataproduct {
func NewDataProductProtectiveBarriers(group string, teamID uuid.UUID) service.NewDataproduct {
return service.NewDataproduct{
Name: "Protective Barriers",
Description: strToStrPtr("Physical barriers to protect coral reefs from human activity"),
Group: GroupEmailNada,
Group: group,
ProductAreaID: &ProductAreaCostalID,
TeamID: &TeamReefID,
TeamID: &teamID,
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/integration/metabase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func TestMetabase(t *testing.T) {
)

StorageCreateProductAreasAndTeams(t, stores.ProductAreaStorage)
fuel, err := dataproductService.CreateDataproduct(ctx, UserOne, NewDataProductBiofuelProduction())
fuel, err := dataproductService.CreateDataproduct(ctx, UserOne, NewDataProductBiofuelProduction(GroupEmailNada, TeamSeagrassID))
assert.NoError(t, err)

fuelData, err := dataproductService.CreateDataset(ctx, UserOne, NewDatasetBiofuelConsumptionRates(fuel.ID))
Expand Down
8 changes: 4 additions & 4 deletions test/integration/productareas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ func TestProductArea(t *testing.T) {
stores := storage.NewStores(repo, config.Config{}, log)

StorageCreateProductAreasAndTeams(t, stores.ProductAreaStorage)
fuel := StorageCreateDataproduct(t, stores.DataProductsStorage, NewDataProductBiofuelProduction())
feed := StorageCreateDataproduct(t, stores.DataProductsStorage, NewDataProductAquacultureFeed())
_ = StorageCreateDataproduct(t, stores.DataProductsStorage, NewDataProductReefMonitoring())
_ = StorageCreateDataproduct(t, stores.DataProductsStorage, NewDataProductProtectiveBarriers())
fuel := StorageCreateDataproduct(t, stores.DataProductsStorage, NewDataProductBiofuelProduction(GroupEmailNada, TeamSeagrassID))
feed := StorageCreateDataproduct(t, stores.DataProductsStorage, NewDataProductAquacultureFeed(GroupEmailNada, TeamSeagrassID))
_ = StorageCreateDataproduct(t, stores.DataProductsStorage, NewDataProductReefMonitoring(GroupEmailNada, TeamReefID))
_ = StorageCreateDataproduct(t, stores.DataProductsStorage, NewDataProductProtectiveBarriers(GroupEmailNada, TeamReefID))

{
s := core.NewProductAreaService(stores.ProductAreaStorage, stores.DataProductsStorage, stores.InsightProductStorage, stores.StoryStorage)
Expand Down
95 changes: 95 additions & 0 deletions test/integration/user_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package integration

import (
"github.com/navikt/nada-backend/pkg/config/v2"
"github.com/navikt/nada-backend/pkg/database"
"github.com/navikt/nada-backend/pkg/service"
"github.com/navikt/nada-backend/pkg/service/core"
"github.com/navikt/nada-backend/pkg/service/core/handlers"
"github.com/navikt/nada-backend/pkg/service/core/routes"
"github.com/navikt/nada-backend/pkg/service/core/storage"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"os"
"testing"
)

func TestUserDataService(t *testing.T) {
log := zerolog.New(os.Stdout)
c := NewContainers(t, log)
defer c.Cleanup()

pgCfg := c.RunPostgres(NewPostgresConfig())

repo, err := database.New(
pgCfg.ConnectionURL(),
10,
10,
)
assert.NoError(t, err)

r := TestRouter(log)

stores := storage.NewStores(repo, config.Config{}, log)

StorageCreateProductAreasAndTeams(t, stores.ProductAreaStorage)
StorageCreateNaisConsoleTeamsAndProjects(t, stores.NaisConsoleStorage, map[string]string{GroupNameNada: GroupNameNada, GroupNameReef: GroupEmailReef})
fuel := StorageCreateDataproduct(t, stores.DataProductsStorage, NewDataProductBiofuelProduction(GroupEmailNada, TeamSeagrassID))
barriers := StorageCreateDataproduct(t, stores.DataProductsStorage, NewDataProductProtectiveBarriers(GroupEmailReef, TeamReefID))
feed := StorageCreateDataproduct(t, stores.DataProductsStorage, NewDataProductAquacultureFeed(GroupEmailNada, TeamSeagrassID))
reef := StorageCreateDataproduct(t, stores.DataProductsStorage, NewDataProductReefMonitoring(GroupEmailReef, TeamReefID))

{
s := core.NewUserService(stores.AccessStorage, stores.TokenStorage, stores.StoryStorage, stores.DataProductsStorage,
stores.InsightProductStorage, stores.NaisConsoleStorage, log)
h := handlers.NewUserHandler(s)
e := routes.NewUserEndpoints(log, h)
f := routes.NewUserRoutes(e, injectUser(&service.User{
Email: "[email protected]",
GoogleGroups: []service.Group{
{
Name: GroupNameNada,
Email: GroupEmailNada,
},
{
Name: GroupNameReef,
Email: GroupEmailReef,
},
},
}))
f(r)
}

server := httptest.NewServer(r)
defer server.Close()
// Would prefer to sort by dp.tema_name, but it is always null
t.Run("User data products are sorted alphabetically by group_name and dp_name", func(t *testing.T) {
got := &service.UserInfo{}
expect := []service.Dataproduct{
{ID: feed.ID, Name: feed.Name, Owner: &service.DataproductOwner{Group: GroupEmailNada}},
{ID: fuel.ID, Name: fuel.Name, Owner: &service.DataproductOwner{Group: GroupEmailNada}},
{ID: barriers.ID, Name: barriers.Name, Owner: &service.DataproductOwner{Group: GroupEmailReef}},
{ID: reef.ID, Name: reef.Name, Owner: &service.DataproductOwner{Group: GroupEmailReef}},
}

NewTester(t, server).Get("/api/userData").
HasStatusCode(http.StatusOK).Value(got)

if len(got.Dataproducts) != len(expect) {
t.Fatalf("got %d, expected %d", len(got.Dataproducts), len(expect))
}
for i := 0; i < len(got.Dataproducts); i++ {
if got.Dataproducts[i].ID != expect[i].ID {
t.Errorf("got %s, expected %s", got.Dataproducts[i].ID, expect[i].ID)
}
if got.Dataproducts[i].Name != expect[i].Name {
t.Errorf("got %s, expected %s", got.Dataproducts[i].Name, expect[i].Name)
}
if got.Dataproducts[i].Owner.Group != expect[i].Owner.Group {
t.Errorf("got %s, expected %s", got.Dataproducts[i].Owner.Group, expect[i].Owner.Group)
}
}
})
}

0 comments on commit 99a5514

Please sign in to comment.