|
7 | 7 | "github.com/stretchr/testify/require"
|
8 | 8 |
|
9 | 9 | "github.com/grafana/grafana/pkg/models"
|
| 10 | + "github.com/grafana/grafana/pkg/util" |
10 | 11 | )
|
11 | 12 |
|
12 | 13 | func TestCreateLibraryElement(t *testing.T) {
|
@@ -59,6 +60,76 @@ func TestCreateLibraryElement(t *testing.T) {
|
59 | 60 | }
|
60 | 61 | })
|
61 | 62 |
|
| 63 | + testScenario(t, "When an admin tries to create a library panel that does not exists using an nonexistent UID, it should succeed", |
| 64 | + func(t *testing.T, sc scenarioContext) { |
| 65 | + command := getCreatePanelCommand(sc.folder.Id, "Nonexistent UID") |
| 66 | + command.UID = util.GenerateShortUID() |
| 67 | + resp := sc.service.createHandler(sc.reqContext, command) |
| 68 | + var result = validateAndUnMarshalResponse(t, resp) |
| 69 | + var expected = libraryElementResult{ |
| 70 | + Result: libraryElement{ |
| 71 | + ID: 1, |
| 72 | + OrgID: 1, |
| 73 | + FolderID: 1, |
| 74 | + UID: command.UID, |
| 75 | + Name: "Nonexistent UID", |
| 76 | + Kind: int64(models.PanelElement), |
| 77 | + Type: "text", |
| 78 | + Description: "A description", |
| 79 | + Model: map[string]interface{}{ |
| 80 | + "datasource": "${DS_GDEV-TESTDATA}", |
| 81 | + "description": "A description", |
| 82 | + "id": float64(1), |
| 83 | + "title": "Text - Library Panel", |
| 84 | + "type": "text", |
| 85 | + }, |
| 86 | + Version: 1, |
| 87 | + Meta: LibraryElementDTOMeta{ |
| 88 | + ConnectedDashboards: 0, |
| 89 | + Created: result.Result.Meta.Created, |
| 90 | + Updated: result.Result.Meta.Updated, |
| 91 | + CreatedBy: LibraryElementDTOMetaUser{ |
| 92 | + ID: 1, |
| 93 | + Name: "signed_in_user", |
| 94 | + AvatarURL: "/avatar/37524e1eb8b3e32850b57db0a19af93b", |
| 95 | + }, |
| 96 | + UpdatedBy: LibraryElementDTOMetaUser{ |
| 97 | + ID: 1, |
| 98 | + Name: "signed_in_user", |
| 99 | + AvatarURL: "/avatar/37524e1eb8b3e32850b57db0a19af93b", |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + } |
| 104 | + if diff := cmp.Diff(expected, result, getCompareOptions()...); diff != "" { |
| 105 | + t.Fatalf("Result mismatch (-want +got):\n%s", diff) |
| 106 | + } |
| 107 | + }) |
| 108 | + |
| 109 | + scenarioWithPanel(t, "When an admin tries to create a library panel that does not exists using an existent UID, it should fail", |
| 110 | + func(t *testing.T, sc scenarioContext) { |
| 111 | + command := getCreatePanelCommand(sc.folder.Id, "Existing UID") |
| 112 | + command.UID = sc.initialResult.Result.UID |
| 113 | + resp := sc.service.createHandler(sc.reqContext, command) |
| 114 | + require.Equal(t, 400, resp.Status()) |
| 115 | + }) |
| 116 | + |
| 117 | + scenarioWithPanel(t, "When an admin tries to create a library panel that does not exists using an invalid UID, it should fail", |
| 118 | + func(t *testing.T, sc scenarioContext) { |
| 119 | + command := getCreatePanelCommand(sc.folder.Id, "Invalid UID") |
| 120 | + command.UID = "Testing an invalid UID" |
| 121 | + resp := sc.service.createHandler(sc.reqContext, command) |
| 122 | + require.Equal(t, 400, resp.Status()) |
| 123 | + }) |
| 124 | + |
| 125 | + scenarioWithPanel(t, "When an admin tries to create a library panel that does not exists using an UID that is too long, it should fail", |
| 126 | + func(t *testing.T, sc scenarioContext) { |
| 127 | + command := getCreatePanelCommand(sc.folder.Id, "Invalid UID") |
| 128 | + command.UID = "j6T00KRZzj6T00KRZzj6T00KRZzj6T00KRZzj6T00K" |
| 129 | + resp := sc.service.createHandler(sc.reqContext, command) |
| 130 | + require.Equal(t, 400, resp.Status()) |
| 131 | + }) |
| 132 | + |
62 | 133 | testScenario(t, "When an admin tries to create a library panel where name and panel title differ, it should not update panel title",
|
63 | 134 | func(t *testing.T, sc scenarioContext) {
|
64 | 135 | command := getCreatePanelCommand(1, "Library Panel Name")
|
|
0 commit comments