Chore: avoid hard-coding IDs in integration tests (#33152)

* use dynamic user Ids in test

* use dynamic IDs in alert tests

* use dynamic IDs in dashboard test queries

* use dynamic IDs in org test queries
This commit is contained in:
Dafydd 2021-04-21 09:22:46 +01:00 committed by GitHub
parent 2cd6c0565b
commit 0dcd0281ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 19 deletions

View File

@ -62,9 +62,17 @@ func TestAlertingDataAccess(t *testing.T) {
})
Convey("Can set new states", func() {
// Get alert so we can use its ID in tests
alertQuery := models.GetAlertsQuery{DashboardIDs: []int64{testDash.Id}, PanelId: 1, OrgId: 1, User: &models.SignedInUser{OrgRole: models.ROLE_ADMIN}}
err2 := HandleAlertsQuery(&alertQuery)
So(err2, ShouldBeNil)
insertedAlert := alertQuery.Result[0]
Convey("new state ok", func() {
cmd := &models.SetAlertStateCommand{
AlertId: 1,
AlertId: insertedAlert.Id,
State: models.AlertStateOK,
}
@ -72,7 +80,7 @@ func TestAlertingDataAccess(t *testing.T) {
So(err, ShouldBeNil)
})
alert, _ := getAlertById(1)
alert, _ := getAlertById(insertedAlert.Id)
stateDateBeforePause := alert.NewStateDate
Convey("can pause all alerts", func() {
@ -81,7 +89,7 @@ func TestAlertingDataAccess(t *testing.T) {
Convey("cannot updated paused alert", func() {
cmd := &models.SetAlertStateCommand{
AlertId: 1,
AlertId: insertedAlert.Id,
State: models.AlertStateOK,
}
@ -90,13 +98,13 @@ func TestAlertingDataAccess(t *testing.T) {
})
Convey("alert is paused", func() {
alert, _ = getAlertById(1)
alert, _ = getAlertById(insertedAlert.Id)
currentState := alert.State
So(currentState, ShouldEqual, "paused")
})
Convey("pausing alerts should update their NewStateDate", func() {
alert, _ = getAlertById(1)
alert, _ = getAlertById(insertedAlert.Id)
stateDateAfterPause := alert.NewStateDate
So(stateDateBeforePause, ShouldHappenBefore, stateDateAfterPause)
})
@ -104,7 +112,7 @@ func TestAlertingDataAccess(t *testing.T) {
Convey("unpausing alerts should update their NewStateDate again", func() {
err := pauseAllAlerts(false)
So(err, ShouldBeNil)
alert, _ = getAlertById(1)
alert, _ = getAlertById(insertedAlert.Id)
stateDateAfterUnpause := alert.NewStateDate
So(stateDateBeforePause, ShouldHappenBefore, stateDateAfterUnpause)
})
@ -259,7 +267,6 @@ func TestAlertingDataAccess(t *testing.T) {
query := models.GetAlertsQuery{DashboardIDs: []int64{testDash.Id}, OrgId: 1, User: &models.SignedInUser{OrgRole: models.ROLE_ADMIN}}
err2 := HandleAlertsQuery(&query)
So(testDash.Id, ShouldEqual, 1)
So(err2, ShouldBeNil)
So(len(query.Result), ShouldEqual, 0)
})
@ -280,12 +287,20 @@ func TestPausingAlerts(t *testing.T) {
stateDateBeforePause := alert.NewStateDate
stateDateAfterPause := stateDateBeforePause
// Get alert so we can use its ID in tests
alertQuery := models.GetAlertsQuery{DashboardIDs: []int64{testDash.Id}, PanelId: 1, OrgId: 1, User: &models.SignedInUser{OrgRole: models.ROLE_ADMIN}}
err2 := HandleAlertsQuery(&alertQuery)
So(err2, ShouldBeNil)
insertedAlert := alertQuery.Result[0]
Convey("when paused", func() {
_, err := pauseAlert(testDash.OrgId, 1, true)
_, err := pauseAlert(testDash.OrgId, insertedAlert.Id, true)
So(err, ShouldBeNil)
Convey("the NewStateDate should be updated", func() {
alert, err := getAlertById(1)
alert, err := getAlertById(insertedAlert.Id)
So(err, ShouldBeNil)
stateDateAfterPause = alert.NewStateDate
@ -294,11 +309,11 @@ func TestPausingAlerts(t *testing.T) {
})
Convey("when unpaused", func() {
_, err := pauseAlert(testDash.OrgId, 1, false)
_, err := pauseAlert(testDash.OrgId, insertedAlert.Id, false)
So(err, ShouldBeNil)
Convey("the NewStateDate should be updated again", func() {
alert, err := getAlertById(1)
alert, err := getAlertById(insertedAlert.Id)
So(err, ShouldBeNil)
stateDateAfterUnpause := alert.NewStateDate

View File

@ -28,7 +28,7 @@ func TestDashboardDataAccess(t *testing.T) {
savedFolder := insertTestDashboard(t, sqlStore, "1 test dash folder", 1, 0, true, "prod", "webapp")
savedDash := insertTestDashboard(t, sqlStore, "test dash 23", 1, savedFolder.Id, false, "prod", "webapp")
insertTestDashboard(t, sqlStore, "test dash 45", 1, savedFolder.Id, false, "prod")
insertTestDashboard(t, sqlStore, "test dash 67", 1, 0, false, "prod", "webapp")
savedDash2 := insertTestDashboard(t, sqlStore, "test dash 67", 1, 0, false, "prod")
Convey("Should return dashboard model", func() {
So(savedDash.Title, ShouldEqual, "test dash 23")
@ -343,7 +343,7 @@ func TestDashboardDataAccess(t *testing.T) {
Convey("Should be able to search for dashboard by dashboard ids", func() {
Convey("should be able to find two dashboards by id", func() {
query := search.FindPersistedDashboardsQuery{
DashboardIds: []int64{2, 3},
DashboardIds: []int64{savedDash.Id, savedDash2.Id},
SignedInUser: &models.SignedInUser{OrgId: 1, OrgRole: models.ROLE_EDITOR},
}

View File

@ -299,7 +299,7 @@ func TestAccountDataAccess(t *testing.T) {
So(err, ShouldBeNil)
Convey("Should remove dependent permissions for deleted org user", func() {
permQuery := &models.GetDashboardAclInfoListQuery{DashboardID: 1, OrgID: ac1.OrgId}
permQuery := &models.GetDashboardAclInfoListQuery{DashboardID: dash1.Id, OrgID: ac1.OrgId}
err = GetDashboardAclInfoList(permQuery)
So(err, ShouldBeNil)
@ -307,7 +307,7 @@ func TestAccountDataAccess(t *testing.T) {
})
Convey("Should not remove dashboard permissions for same user in another org", func() {
permQuery := &models.GetDashboardAclInfoListQuery{DashboardID: 2, OrgID: ac3.OrgId}
permQuery := &models.GetDashboardAclInfoListQuery{DashboardID: dash2.Id, OrgID: ac3.OrgId}
err = GetDashboardAclInfoList(permQuery)
So(err, ShouldBeNil)

View File

@ -363,7 +363,7 @@ func TestUserDataAccess(t *testing.T) {
require.True(t, found)
disableCmd := models.BatchDisableUsersCommand{
UserIds: []int64{1, 2, 3, 4, 5},
UserIds: []int64{users[0].Id, users[1].Id, users[2].Id, users[3].Id, users[4].Id},
IsDisabled: true,
}
@ -406,7 +406,7 @@ func TestUserDataAccess(t *testing.T) {
t.Run("Testing DB - enable all users", func(t *testing.T) {
createFiveTestUsers(t, ss, func(i int) *models.CreateUserCommand {
users := createFiveTestUsers(t, ss, func(i int) *models.CreateUserCommand {
return &models.CreateUserCommand{
Email: fmt.Sprint("user", i, "@test.com"),
Name: fmt.Sprint("user", i),
@ -416,7 +416,7 @@ func TestUserDataAccess(t *testing.T) {
})
disableCmd := models.BatchDisableUsersCommand{
UserIds: []int64{1, 2, 3, 4, 5},
UserIds: []int64{users[0].Id, users[1].Id, users[2].Id, users[3].Id, users[4].Id},
IsDisabled: false,
}
@ -607,7 +607,7 @@ func TestUserDataAccess(t *testing.T) {
require.Nil(t, err)
// Cannot make themselves a non-admin
updatePermsError := ss.UpdateUserPermissions(1, false)
updatePermsError := ss.UpdateUserPermissions(user.Id, false)
require.Equal(t, updatePermsError, models.ErrLastGrafanaAdmin)