Skip to content

Commit

Permalink
Add more tests, fix bugs found by tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iansltx committed Jan 10, 2025
1 parent 3306f85 commit a384c97
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion server/datastore/mysql/vpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ func (ds *Datastore) GetTitleInfoFromVPPAppsTeamsID(ctx context.Context, vppApps
}

func (ds *Datastore) GetVPPAppMetadataByAdamIDAndPlatform(ctx context.Context, adamID string, platform fleet.AppleDevicePlatform) (*fleet.VPPApp, error) {
stmt := `SELECT va.adam_id, va.bundle_identifier, va.icon_url, va.name, va.title_id, va.platform, va.created_at, va.updated_at,
stmt := `SELECT va.adam_id, va.bundle_identifier, va.icon_url, va.name, va.title_id, va.platform, va.created_at, va.updated_at
FROM vpp_apps va WHERE va.adam_id = ? AND va.platform = ?
`

Expand Down
10 changes: 10 additions & 0 deletions server/datastore/mysql/vpp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,14 @@ func testVPPAppMetadata(t *testing.T, ds *Datastore) {
meta.VPPAppsTeamsID = 0 // we don't care about the VPP app team PK
require.Equal(t, &fleet.VPPAppStoreApp{Name: "vpp2", VPPAppID: vpp2}, meta)

appMeta, err := ds.GetVPPAppMetadataByAdamIDAndPlatform(ctx, meta.AdamID, meta.Platform)
require.NoError(t, err)
require.Equal(t, appMeta.AdamID, meta.AdamID)
require.Equal(t, appMeta.Platform, meta.Platform)

_, err = ds.GetVPPAppMetadataByAdamIDAndPlatform(ctx, "foo", meta.Platform)
require.ErrorContains(t, err, "not found")

// mark it as install_during_setup for team 2
ExecAdhocSQL(t, ds, func(q sqlx.ExtContext) error {
_, err := q.ExecContext(ctx, `UPDATE vpp_apps_teams SET install_during_setup = 1 WHERE global_or_team_id = ? AND adam_id = ?`, team2.ID, vpp2.AdamID)
Expand Down Expand Up @@ -911,6 +919,8 @@ func testVPPTokensCRUD(t *testing.T, ds *Datastore) {
assert.Equal(t, team.ID, teamTok.Teams[0].ID)
assert.Equal(t, team.Name, teamTok.Teams[0].Name)

// TODO make sure policies are unaffected

// Renew flow
upTok, err = ds.UpdateVPPToken(ctx, tokID, dataToken6)
assert.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion server/service/team_policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ func (svc *Service) getInstallerOrVPPAppForTitle(ctx context.Context, teamID *ui
if softwareTitle.AppStoreApp.Platform != fleet.MacOSPlatform {
return nil, nil, ctxerr.Wrap(ctx, &fleet.BadRequestError{
Message: fmt.Sprintf(
"software_title_id %d on team_id %d is assocated to an iOS or iPadOS VPP app, only software installers or macOS VPP apps are supported",
"software_title_id %d on team_id %d is associated to an iOS or iPadOS VPP app, only software installers or macOS VPP apps are supported",
*softwareTitleID,
*teamID,
),
Expand Down
25 changes: 25 additions & 0 deletions server/service/team_policies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,31 @@ func TestTeamPoliciesAuth(t *testing.T) {
}
}

func TestTeamPolicyVPPAutomationRejectsNonMacOS(t *testing.T) {
ds := new(mock.Store)
svc, ctx := newTestService(t, ds, nil, nil)
ctx = viewer.NewContext(ctx, viewer.Viewer{User: &fleet.User{GlobalRole: ptr.String(fleet.RoleAdmin)}})

appID := fleet.VPPAppID{AdamID: "123456", Platform: fleet.IOSPlatform}
ds.TeamExistsFunc = func(ctx context.Context, id uint) (bool, error) {
return true, nil
}
ds.SoftwareTitleByIDFunc = func(ctx context.Context, id uint, teamID *uint, tmFilter fleet.TeamFilter) (*fleet.SoftwareTitle, error) {
return &fleet.SoftwareTitle{
AppStoreApp: &fleet.VPPAppStoreApp{
VPPAppID: appID,
},
}, nil
}

_, err := svc.NewTeamPolicy(ctx, 1, fleet.NewTeamPolicyPayload{
Name: "query1",
Query: "select 1;",
SoftwareTitleID: ptr.Uint(123),
})
require.ErrorContains(t, err, "is associated to an iOS or iPadOS VPP app")
}

func checkAuthErr(t *testing.T, shouldFail bool, err error) {
t.Helper()
if shouldFail {
Expand Down

0 comments on commit a384c97

Please sign in to comment.