@ -205,17 +205,18 @@ func TestCreatePublicDashboard(t *testing.T) {
serviceWrapper : serviceWrapper ,
}
isEnabled , annotationsEnabled , timeSelectionEnabled := true , false , true
dto := & SavePublicDashboardDTO {
DashboardUid : dashboard . UID ,
OrgId : dashboard . OrgID ,
UserId : 7 ,
PublicDashboard : & PublicDashboard {
IsEnabled : true ,
AnnotationsEnabled : false ,
TimeSelectionEnabled : true ,
PublicDashboard : & PublicDashboardDTO {
IsEnabled : & isEnabled ,
AnnotationsEnabled : & annotationsEnabled ,
TimeSelectionEnabled : & timeSelectionEnabled ,
Share : EmailShareType ,
DashboardUid : "NOTTHESAME" ,
OrgId : 9999999 ,
OrgId : dashboard . OrgID ,
TimeSettings : timeSettings ,
} ,
}
@ -230,10 +231,10 @@ func TestCreatePublicDashboard(t *testing.T) {
assert . Equal ( t , dashboard . UID , pubdash . DashboardUid )
assert . Equal ( t , dashboard . OrgID , pubdash . OrgId )
assert . Equal ( t , dto . UserId , pubdash . CreatedBy )
assert . Equal ( t , dto . PublicDashboard . AnnotationsEnabled , pubdash . AnnotationsEnabled )
assert . Equal ( t , dto . PublicDashboard . TimeSelectionEnabled , pubdash . TimeSelectionEnabled )
assert . Equal ( t , * dto . PublicDashboard . AnnotationsEnabled , pubdash . AnnotationsEnabled )
assert . Equal ( t , * dto . PublicDashboard . TimeSelectionEnabled , pubdash . TimeSelectionEnabled )
// ExistsEnabledByDashboardUid set by parameters
assert . Equal ( t , dto . PublicDashboard . IsEnabled , pubdash . IsEnabled )
assert . Equal ( t , * dto . PublicDashboard . IsEnabled , pubdash . IsEnabled )
// CreatedAt set to non-zero time
assert . NotEqual ( t , & time . Time { } , pubdash . CreatedAt )
// Time settings set by db
@ -244,6 +245,81 @@ func TestCreatePublicDashboard(t *testing.T) {
require . NoError ( t , err , "expected a valid UUID, got %s" , pubdash . AccessToken )
} )
trueBooleanField := true
testCases := [ ] struct {
Name string
IsEnabled * bool
TimeSelectionEnabled * bool
AnnotationsEnabled * bool
} {
{
Name : "isEnabled" ,
IsEnabled : nil ,
TimeSelectionEnabled : & trueBooleanField ,
AnnotationsEnabled : & trueBooleanField ,
} ,
{
Name : "timeSelectionEnabled" ,
IsEnabled : & trueBooleanField ,
TimeSelectionEnabled : nil ,
AnnotationsEnabled : & trueBooleanField ,
} ,
{
Name : "annotationsEnabled" ,
IsEnabled : & trueBooleanField ,
TimeSelectionEnabled : & trueBooleanField ,
AnnotationsEnabled : nil ,
} ,
{
Name : "isEnabled, timeSelectionEnabled and annotationsEnabled" ,
IsEnabled : nil ,
TimeSelectionEnabled : nil ,
AnnotationsEnabled : nil ,
} ,
}
for _ , tt := range testCases {
t . Run ( fmt . Sprintf ( "Create public dashboard with %s null boolean fields stores them as false" , tt . Name ) , func ( t * testing . T ) {
sqlStore := db . InitTestDB ( t )
quotaService := quotatest . New ( false , nil )
dashboardStore , err := dashboardsDB . ProvideDashboardStore ( sqlStore , sqlStore . Cfg , featuremgmt . WithFeatures ( ) , tagimpl . ProvideService ( sqlStore , sqlStore . Cfg ) , quotaService )
require . NoError ( t , err )
publicdashboardStore := database . ProvideStore ( sqlStore )
dashboard := insertTestDashboard ( t , dashboardStore , "testDashie" , 1 , 0 , true , [ ] map [ string ] interface { } { } , nil )
serviceWrapper := ProvideServiceWrapper ( publicdashboardStore )
service := & PublicDashboardServiceImpl {
log : log . New ( "test.logger" ) ,
store : publicdashboardStore ,
serviceWrapper : serviceWrapper ,
}
dto := & SavePublicDashboardDTO {
DashboardUid : dashboard . UID ,
UserId : 7 ,
PublicDashboard : & PublicDashboardDTO {
IsEnabled : tt . IsEnabled ,
TimeSelectionEnabled : tt . TimeSelectionEnabled ,
AnnotationsEnabled : tt . AnnotationsEnabled ,
Share : PublicShareType ,
DashboardUid : "NOTTHESAME" ,
OrgId : dashboard . OrgID ,
TimeSettings : timeSettings ,
} ,
}
_ , err = service . Create ( context . Background ( ) , SignedInUser , dto )
require . NoError ( t , err )
pubdash , err := service . FindByDashboardUid ( context . Background ( ) , dashboard . OrgID , dashboard . UID )
require . NoError ( t , err )
assertFalseIfNull ( t , pubdash . IsEnabled , dto . PublicDashboard . IsEnabled )
assertFalseIfNull ( t , pubdash . TimeSelectionEnabled , dto . PublicDashboard . TimeSelectionEnabled )
assertFalseIfNull ( t , pubdash . AnnotationsEnabled , dto . PublicDashboard . AnnotationsEnabled )
} )
}
t . Run ( "Validate pubdash has default time setting value" , func ( t * testing . T ) {
sqlStore := db . InitTestDB ( t )
quotaService := quotatest . New ( false , nil )
@ -259,14 +335,14 @@ func TestCreatePublicDashboard(t *testing.T) {
serviceWrapper : serviceWrapper ,
}
isEnabled := true
dto := & SavePublicDashboardDTO {
DashboardUid : dashboard . UID ,
OrgId : dashboard . OrgID ,
UserId : 7 ,
PublicDashboard : & PublicDashboard {
IsEnabled : true ,
PublicDashboard : & PublicDashboardDTO {
IsEnabled : & isEnabled ,
DashboardUid : "NOTTHESAME" ,
OrgId : 9999999 ,
OrgId : dashboard . OrgID ,
} ,
}
@ -294,14 +370,14 @@ func TestCreatePublicDashboard(t *testing.T) {
serviceWrapper : serviceWrapper ,
}
isEnabled := true
dto := & SavePublicDashboardDTO {
DashboardUid : dashboard . UID ,
OrgId : dashboard . OrgID ,
UserId : 7 ,
PublicDashboard : & PublicDashboard {
IsEnabled : true ,
PublicDashboard : & PublicDashboardDTO {
IsEnabled : & isEnabled ,
DashboardUid : "NOTTHESAME" ,
OrgId : 9999999 ,
OrgId : dashboard . OrgID ,
} ,
}
@ -321,7 +397,7 @@ func TestCreatePublicDashboard(t *testing.T) {
IsEnabled : true ,
AnnotationsEnabled : false ,
DashboardUid : "NOTTHESAME" ,
OrgId : 9999999 ,
OrgId : dashboard . OrgID ,
TimeSettings : timeSettings ,
}
@ -339,14 +415,14 @@ func TestCreatePublicDashboard(t *testing.T) {
serviceWrapper : serviceWrapper ,
}
isEnabled := true
dto := & SavePublicDashboardDTO {
DashboardUid : "an-id" ,
OrgId : 8 ,
UserId : 7 ,
PublicDashboard : & PublicDashboard {
IsEnabled : true ,
PublicDashboard : & PublicDashboardDTO {
IsEnabled : & isEnabled ,
DashboardUid : "NOTTHESAME" ,
OrgId : 9999999 ,
OrgId : dashboard . OrgID ,
} ,
}
@ -376,13 +452,13 @@ func TestCreatePublicDashboard(t *testing.T) {
serviceWrapper : serviceWrapper ,
}
isEnabled , annotationsEnabled := true , false
dto := & SavePublicDashboardDTO {
DashboardUid : dashboard . UID ,
OrgId : dashboard . OrgID ,
UserId : 7 ,
PublicDashboard : & PublicDashboard {
AnnotationsEnabled : false ,
IsEnabled : true ,
PublicDashboard : & PublicDashboardDTO {
AnnotationsEnabled : & annotationsEnabled ,
IsEnabled : & isEnabled ,
TimeSettings : timeSettings ,
} ,
}
@ -408,14 +484,14 @@ func TestCreatePublicDashboard(t *testing.T) {
serviceWrapper : serviceWrapper ,
}
isEnabled := true
dto := & SavePublicDashboardDTO {
DashboardUid : dashboard . UID ,
OrgId : dashboard . OrgID ,
UserId : 7 ,
PublicDashboard : & PublicDashboard {
IsEnabled : true ,
PublicDashboard : & PublicDashboardDTO {
IsEnabled : & isEnabled ,
DashboardUid : "NOTTHESAME" ,
OrgId : 9999999 ,
OrgId : dashboard . OrgID ,
} ,
}
@ -429,6 +505,14 @@ func TestCreatePublicDashboard(t *testing.T) {
} )
}
func assertFalseIfNull ( t * testing . T , expectedValue bool , nullableValue * bool ) {
if nullableValue == nil {
assert . Equal ( t , expectedValue , false )
} else {
assert . Equal ( t , expectedValue , * nullableValue )
}
}
func TestUpdatePublicDashboard ( t * testing . T ) {
t . Run ( "Updating public dashboard" , func ( t * testing . T ) {
sqlStore := db . InitTestDB ( t )
@ -445,14 +529,14 @@ func TestUpdatePublicDashboard(t *testing.T) {
serviceWrapper : serviceWrapper ,
}
isEnabled , annotationsEnabled , timeSelectionEnabled := true , false , false
dto := & SavePublicDashboardDTO {
DashboardUid : dashboard . UID ,
OrgId : dashboard . OrgID ,
UserId : 7 ,
PublicDashboard : & PublicDashboard {
AnnotationsEnabled : false ,
IsEnabled : true ,
TimeSelectionEnabled : false ,
PublicDashboard : & PublicDashboardDTO {
IsEnabled : & isEnabled ,
AnnotationsEnabled : & annotationsEnabled ,
TimeSelectionEnabled : & timeSelectionEnabled ,
TimeSettings : timeSettings ,
} ,
}
@ -461,21 +545,21 @@ func TestUpdatePublicDashboard(t *testing.T) {
savedPubdash , err := service . Create ( context . Background ( ) , SignedInUser , dto )
require . NoError ( t , err )
isEnabled , annotationsEnabled , timeSelectionEnabled = true , true , true
// attempt to overwrite settings
dto = & SavePublicDashboardDTO {
DashboardUid : dashboard . UID ,
OrgId : dashboard . OrgID ,
UserId : 8 ,
PublicDashboard : & PublicDashboard {
PublicDashboard : & PublicDashboardDTO {
Uid : savedPubdash . Uid ,
OrgId : 9 ,
DashboardUid : "abc1234" ,
CreatedBy : 9 ,
CreatedAt : time . Time { } ,
IsEnabled : true ,
AnnotationsEnabled : true ,
TimeSelectionEnabled : true ,
IsEnabled : & isEnabled ,
AnnotationsEnabled : & annotationsEnabled ,
TimeSelectionEnabled : & timeSelectionEnabled ,
TimeSettings : timeSettings ,
AccessToken : "NOTAREALUUID" ,
} ,
@ -491,9 +575,9 @@ func TestUpdatePublicDashboard(t *testing.T) {
assert . Equal ( t , savedPubdash . AccessToken , updatedPubdash . AccessToken )
// gets updated
assert . Equal ( t , dto . PublicDashboard . IsEnabled , updatedPubdash . IsEnabled )
assert . Equal ( t , dto . PublicDashboard . AnnotationsEnabled , updatedPubdash . AnnotationsEnabled )
assert . Equal ( t , dto . PublicDashboard . TimeSelectionEnabled , updatedPubdash . TimeSelectionEnabled )
assert . Equal ( t , * dto . PublicDashboard . IsEnabled , updatedPubdash . IsEnabled )
assert . Equal ( t , * dto . PublicDashboard . AnnotationsEnabled , updatedPubdash . AnnotationsEnabled )
assert . Equal ( t , * dto . PublicDashboard . TimeSelectionEnabled , updatedPubdash . TimeSelectionEnabled )
assert . Equal ( t , dto . PublicDashboard . TimeSettings , updatedPubdash . TimeSettings )
assert . Equal ( t , dto . UserId , updatedPubdash . UpdatedBy )
assert . NotEqual ( t , & time . Time { } , updatedPubdash . UpdatedAt )
@ -515,12 +599,12 @@ func TestUpdatePublicDashboard(t *testing.T) {
serviceWrapper : serviceWrapper ,
}
isEnabled := true
dto := & SavePublicDashboardDTO {
DashboardUid : dashboard . UID ,
OrgId : dashboard . OrgID ,
UserId : 7 ,
PublicDashboard : & PublicDashboard {
IsEnabled : true ,
PublicDashboard : & PublicDashboardDTO {
IsEnabled : & isEnabled ,
TimeSettings : timeSettings ,
} ,
}
@ -531,17 +615,16 @@ func TestUpdatePublicDashboard(t *testing.T) {
// attempt to overwrite settings
dto = & SavePublicDashboardDTO {
DashboardUid : dashboard . UID ,
OrgId : dashboard . OrgID ,
UserId : 8 ,
PublicDashboard : & PublicDashboard {
PublicDashboard : & PublicDashboardDTO {
Uid : savedPubdash . Uid ,
OrgId : 9 ,
DashboardUid : "abc1234" ,
CreatedBy : 9 ,
CreatedAt : time . Time { } ,
IsEnabled : true ,
AccessToken : "NOTAREALUUID" ,
IsEnabled : & isEnabled ,
TimeSettings : & TimeSettings { } ,
AccessToken : "NOTAREALUUID" ,
} ,
}
@ -550,6 +633,132 @@ func TestUpdatePublicDashboard(t *testing.T) {
assert . Equal ( t , & TimeSettings { } , updatedPubdash . TimeSettings )
} )
trueBooleanField := true
timeSettings := & TimeSettings { From : "now-8" , To : "now" }
shareType := EmailShareType
testCases := [ ] struct {
Name string
IsEnabled * bool
TimeSelectionEnabled * bool
AnnotationsEnabled * bool
TimeSettings * TimeSettings
ShareType ShareType
} {
{
Name : "isEnabled" ,
IsEnabled : nil ,
TimeSelectionEnabled : & trueBooleanField ,
AnnotationsEnabled : & trueBooleanField ,
TimeSettings : timeSettings ,
ShareType : shareType ,
} ,
{
Name : "timeSelectionEnabled" ,
IsEnabled : & trueBooleanField ,
TimeSelectionEnabled : nil ,
AnnotationsEnabled : & trueBooleanField ,
TimeSettings : timeSettings ,
ShareType : shareType ,
} ,
{
Name : "annotationsEnabled" ,
IsEnabled : & trueBooleanField ,
TimeSelectionEnabled : & trueBooleanField ,
AnnotationsEnabled : nil ,
TimeSettings : timeSettings ,
ShareType : shareType ,
} ,
{
Name : "isEnabled, timeSelectionEnabled and annotationsEnabled" ,
IsEnabled : nil ,
TimeSelectionEnabled : nil ,
AnnotationsEnabled : nil ,
TimeSettings : nil ,
ShareType : "" ,
} ,
}
for _ , tt := range testCases {
t . Run ( fmt . Sprintf ( "Update public dashboard with %s null boolean fields let those fields with old persisted value" , tt . Name ) , func ( t * testing . T ) {
sqlStore := db . InitTestDB ( t )
quotaService := quotatest . New ( false , nil )
dashboardStore , err := dashboardsDB . ProvideDashboardStore ( sqlStore , sqlStore . Cfg , featuremgmt . WithFeatures ( ) , tagimpl . ProvideService ( sqlStore , sqlStore . Cfg ) , quotaService )
require . NoError ( t , err )
publicdashboardStore := database . ProvideStore ( sqlStore )
serviceWrapper := ProvideServiceWrapper ( publicdashboardStore )
dashboard := insertTestDashboard ( t , dashboardStore , "testDashie" , 1 , 0 , true , [ ] map [ string ] interface { } { } , nil )
service := & PublicDashboardServiceImpl {
log : log . New ( "test.logger" ) ,
store : publicdashboardStore ,
serviceWrapper : serviceWrapper ,
}
isEnabled , annotationsEnabled , timeSelectionEnabled := true , true , false
dto := & SavePublicDashboardDTO {
DashboardUid : dashboard . UID ,
UserId : 7 ,
PublicDashboard : & PublicDashboardDTO {
IsEnabled : & isEnabled ,
AnnotationsEnabled : & annotationsEnabled ,
TimeSelectionEnabled : & timeSelectionEnabled ,
TimeSettings : timeSettings ,
Share : PublicShareType ,
} ,
}
// insert initial pubdash
savedPubdash , err := service . Create ( context . Background ( ) , SignedInUser , dto )
require . NoError ( t , err )
// attempt to overwrite settings
dto = & SavePublicDashboardDTO {
DashboardUid : dashboard . UID ,
UserId : 8 ,
PublicDashboard : & PublicDashboardDTO {
Uid : savedPubdash . Uid ,
OrgId : 9 ,
DashboardUid : "abc1234" ,
CreatedBy : 9 ,
CreatedAt : time . Time { } ,
IsEnabled : tt . IsEnabled ,
AnnotationsEnabled : tt . AnnotationsEnabled ,
TimeSelectionEnabled : tt . TimeSelectionEnabled ,
TimeSettings : tt . TimeSettings ,
Share : tt . ShareType ,
AccessToken : "NOTAREALUUID" ,
} ,
}
updatedPubdash , err := service . Update ( context . Background ( ) , SignedInUser , dto )
require . NoError ( t , err )
assertOldValueIfNull ( t , updatedPubdash . IsEnabled , savedPubdash . IsEnabled , dto . PublicDashboard . IsEnabled )
assertOldValueIfNull ( t , updatedPubdash . AnnotationsEnabled , savedPubdash . AnnotationsEnabled , dto . PublicDashboard . AnnotationsEnabled )
assertOldValueIfNull ( t , updatedPubdash . TimeSelectionEnabled , savedPubdash . TimeSelectionEnabled , dto . PublicDashboard . TimeSelectionEnabled )
if dto . PublicDashboard . TimeSettings == nil {
assert . Equal ( t , updatedPubdash . TimeSettings , savedPubdash . TimeSettings )
} else {
assert . Equal ( t , updatedPubdash . TimeSettings , dto . PublicDashboard . TimeSettings )
}
if dto . PublicDashboard . Share == "" {
assert . Equal ( t , updatedPubdash . Share , savedPubdash . Share )
} else {
assert . Equal ( t , updatedPubdash . Share , dto . PublicDashboard . Share )
}
} )
}
}
func assertOldValueIfNull ( t * testing . T , expectedValue bool , oldValue bool , nullableValue * bool ) {
if nullableValue == nil {
assert . Equal ( t , expectedValue , oldValue )
} else {
assert . Equal ( t , expectedValue , * nullableValue )
}
}
func TestDeletePublicDashboard ( t * testing . T ) {