|
| 1 | +package gapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/gobs/pretty" |
| 7 | +) |
| 8 | + |
| 9 | +const ( |
| 10 | + createPublicDashboard = `{ |
| 11 | + "uid": "fdc8b8fd-72cb-45d2-927a-75900e4f6e70", |
| 12 | + "dashboardUid": "nErXDvCkzz", |
| 13 | + "isEnabled": true, |
| 14 | + "share": "public" |
| 15 | +}` |
| 16 | + updatePublicDashboard = `{ |
| 17 | + "timeSelectionEnabled": true, |
| 18 | + "isEnabled": true, |
| 19 | + "annotationsEnabled": true |
| 20 | +}` |
| 21 | + publicDashboardByUID = `{ |
| 22 | + "uid": "cd56d9fd-f3d4-486d-afba-a21760e2acbe", |
| 23 | + "dashboardUid": "xCpsVuc4z", |
| 24 | + "accessToken": "5c948bf96e6a4b13bd91975f9a2028b7", |
| 25 | + "createdBy": 1, |
| 26 | + "updatedBy": 1, |
| 27 | + "createdAt": "2023-09-05T11:41:14-03:00", |
| 28 | + "updatedAt": "2023-09-05T11:41:14-03:00", |
| 29 | + "timeSelectionEnabled": false, |
| 30 | + "isEnabled": true, |
| 31 | + "annotationsEnabled": false, |
| 32 | + "share": "public" |
| 33 | +}` |
| 34 | + publicDashboardList = `{ |
| 35 | + "publicDashboards": [ |
| 36 | + { |
| 37 | + "uid": "e9f29a3c-fcc3-4fc5-a690-ae39c97d24ba", |
| 38 | + "accessToken": "6c13ec1997ba48c5af8c9c5079049692", |
| 39 | + "title": "A Datasource not found query", |
| 40 | + "dashboardUid": "d2f21d0a-76c7-47ec-b5f3-9dda16e5a996", |
| 41 | + "isEnabled": true |
| 42 | + }, |
| 43 | + { |
| 44 | + "uid": "a174f604-6fe7-47de-97b4-48b7e401b540", |
| 45 | + "accessToken": "d1fcff345c0f45e8a78c096c9696034a", |
| 46 | + "title": "A Issue heatmap bargauge panel", |
| 47 | + "dashboardUid": "51DiOw0Vz", |
| 48 | + "isEnabled": true |
| 49 | + } |
| 50 | + ], |
| 51 | + "totalCount": 2, |
| 52 | + "page": 1, |
| 53 | + "perPage": 1000 |
| 54 | +}` |
| 55 | +) |
| 56 | + |
| 57 | +func TestNewPublicDashboard(t *testing.T) { |
| 58 | + const dashboardUID = "nErXDvCkzz" |
| 59 | + |
| 60 | + client := gapiTestTools(t, 200, createPublicDashboard) |
| 61 | + |
| 62 | + publicDashboard := PublicDashboardPayload{ |
| 63 | + UID: "fdc8b8fd-72cb-45d2-927a-75900e4f6e70", |
| 64 | + AccessToken: "b1d5f3f534d84375a897f3969b6157f3", |
| 65 | + IsEnabled: true, |
| 66 | + Share: "public", |
| 67 | + } |
| 68 | + |
| 69 | + resp, err := client.NewPublicDashboard(dashboardUID, publicDashboard) |
| 70 | + if err != nil { |
| 71 | + t.Fatal(err) |
| 72 | + } |
| 73 | + |
| 74 | + t.Log(pretty.PrettyFormat(resp)) |
| 75 | + |
| 76 | + if resp.UID != "fdc8b8fd-72cb-45d2-927a-75900e4f6e70" { |
| 77 | + t.Errorf("Invalid uid - %s, Expected %s", resp.UID, "fdc8b8fd-72cb-45d2-927a-75900e4f6e70") |
| 78 | + } |
| 79 | + |
| 80 | + if resp.DashboardUID != dashboardUID { |
| 81 | + t.Errorf("Invalid dashboard uid - %s, Expected %s", resp.DashboardUID, dashboardUID) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func TestDeletePublicDashboard(t *testing.T) { |
| 86 | + client := gapiTestTools(t, 200, "") |
| 87 | + |
| 88 | + err := client.DeletePublicDashboard("nErXDvCkza", "fdc8b8fd-72cb-45d2-927a-75900e4f6e70") |
| 89 | + if err != nil { |
| 90 | + t.Error(err) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestPublicDashboards(t *testing.T) { |
| 95 | + client := gapiTestTools(t, 200, publicDashboardList) |
| 96 | + |
| 97 | + resp, err := client.PublicDashboards() |
| 98 | + if err != nil { |
| 99 | + t.Fatal(err) |
| 100 | + } |
| 101 | + |
| 102 | + t.Log(pretty.PrettyFormat(resp)) |
| 103 | + |
| 104 | + if len(resp.PublicDashboards) != 2 || resp.TotalCount != 2 { |
| 105 | + t.Error("Length of returned public dashboards should be 2") |
| 106 | + } |
| 107 | + if resp.PublicDashboards[0].UID != "e9f29a3c-fcc3-4fc5-a690-ae39c97d24ba" || resp.PublicDashboards[0].AccessToken != "6c13ec1997ba48c5af8c9c5079049692" { |
| 108 | + t.Error("Not correctly parsing returned public dashboards.") |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func TestPublicDashboardByUID(t *testing.T) { |
| 113 | + client := gapiTestTools(t, 200, publicDashboardByUID) |
| 114 | + |
| 115 | + resp, err := client.PublicDashboardbyUID("xCpsVuc4z") |
| 116 | + if err != nil { |
| 117 | + t.Fatal(err) |
| 118 | + } |
| 119 | + |
| 120 | + t.Log(pretty.PrettyFormat(resp)) |
| 121 | + |
| 122 | + if resp.UID != "cd56d9fd-f3d4-486d-afba-a21760e2acbe" { |
| 123 | + t.Errorf("Invalid uid - %s, Expected %s", resp.UID, "cd56d9fd-f3d4-486d-afba-a21760e2acbe") |
| 124 | + } |
| 125 | + |
| 126 | + if resp.DashboardUID != "xCpsVuc4z" { |
| 127 | + t.Errorf("Invalid dashboard uid - %s, Expected %s", resp.DashboardUID, "xCpsVuc4z") |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func TestUpdatePublicDashboard(t *testing.T) { |
| 132 | + client := gapiTestTools(t, 200, updatePublicDashboard) |
| 133 | + |
| 134 | + publicDashboard := PublicDashboardPayload{ |
| 135 | + IsEnabled: true, |
| 136 | + TimeSelectionEnabled: true, |
| 137 | + AnnotationsEnabled: true, |
| 138 | + } |
| 139 | + |
| 140 | + resp, err := client.UpdatePublicDashboard("xCpsVuc4z", "cd56d9fd-f3d4-486d-afba-a21760e2acbe", publicDashboard) |
| 141 | + if err != nil { |
| 142 | + t.Fatal(err) |
| 143 | + } |
| 144 | + |
| 145 | + t.Log(pretty.PrettyFormat(resp)) |
| 146 | + |
| 147 | + if !resp.IsEnabled { |
| 148 | + t.Errorf("Invalid IsEnabled - %t, Expected %t", resp.IsEnabled, true) |
| 149 | + } |
| 150 | + |
| 151 | + if !resp.TimeSelectionEnabled { |
| 152 | + t.Errorf("Invalid TimeSelectionEnabled - %t, Expected %t", resp.TimeSelectionEnabled, true) |
| 153 | + } |
| 154 | + |
| 155 | + if !resp.AnnotationsEnabled { |
| 156 | + t.Errorf("Invalid AnnotationsEnabled - %t, Expected %t", resp.AnnotationsEnabled, true) |
| 157 | + } |
| 158 | +} |
0 commit comments