|
| 1 | +package models_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/grafana/github-datasource/pkg/models" |
| 9 | + "github.com/grafana/grafana-plugin-sdk-go/backend" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func TestLoadSettings(t *testing.T) { |
| 15 | + tests := []struct { |
| 16 | + name string |
| 17 | + settings backend.DataSourceInstanceSettings |
| 18 | + jsonData json.RawMessage |
| 19 | + decryptedJsonData map[string]string |
| 20 | + want models.Settings |
| 21 | + wantErr error |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "valid config should not throw error for pat authentication", |
| 25 | + jsonData: []byte(`{ |
| 26 | + "githubUrl" : "https://foo.com" |
| 27 | + }`), |
| 28 | + decryptedJsonData: map[string]string{"accessToken": "foo"}, |
| 29 | + want: models.Settings{ |
| 30 | + GitHubURL: "https://foo.com", |
| 31 | + SelectedAuthType: models.AuthTypePAT, |
| 32 | + AccessToken: "foo", |
| 33 | + }, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "valid config should not throw error for github app type authentication", |
| 37 | + jsonData: []byte(`{ |
| 38 | + "githubUrl" : "https://foo.com", |
| 39 | + "selectedAuthType" : "github-app", |
| 40 | + "appId" : "1111", |
| 41 | + "installationId" : "2222" |
| 42 | + }`), |
| 43 | + decryptedJsonData: map[string]string{"privateKey": "foo"}, |
| 44 | + want: models.Settings{ |
| 45 | + GitHubURL: "https://foo.com", |
| 46 | + SelectedAuthType: models.AuthTypeGithubApp, |
| 47 | + AppId: []byte(`"1111"`), |
| 48 | + AppIdInt64: 1111, |
| 49 | + InstallationId: []byte(`"2222"`), |
| 50 | + InstallationIdInt64: 2222, |
| 51 | + PrivateKey: "foo", |
| 52 | + }, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "valid config should not throw error for github app type authentication - passed as numbers", |
| 56 | + jsonData: []byte(`{ |
| 57 | + "githubUrl" : "https://foo.com", |
| 58 | + "selectedAuthType" : "github-app", |
| 59 | + "appId" : 1111, |
| 60 | + "installationId" : 2222 |
| 61 | + }`), |
| 62 | + decryptedJsonData: map[string]string{"privateKey": "foo"}, |
| 63 | + want: models.Settings{ |
| 64 | + GitHubURL: "https://foo.com", |
| 65 | + SelectedAuthType: models.AuthTypeGithubApp, |
| 66 | + AppId: []byte(`1111`), |
| 67 | + AppIdInt64: 1111, |
| 68 | + InstallationId: []byte(`2222`), |
| 69 | + InstallationIdInt64: 2222, |
| 70 | + PrivateKey: "foo", |
| 71 | + }, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "invalid config should throw error for github app type authentication - app id passed as string literals", |
| 75 | + jsonData: []byte(`{ |
| 76 | + "githubUrl" : "https://foo.com", |
| 77 | + "selectedAuthType" : "github-app", |
| 78 | + "appId" : "1111xyz", |
| 79 | + "installationId" : "2222" |
| 80 | + }`), |
| 81 | + decryptedJsonData: map[string]string{"privateKey": "foo"}, |
| 82 | + wantErr: errors.New("error parsing app id"), |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "invalid config should throw error for github app type authentication - installation id passed as string literals", |
| 86 | + jsonData: []byte(`{ |
| 87 | + "githubUrl" : "https://foo.com", |
| 88 | + "selectedAuthType" : "github-app", |
| 89 | + "appId" : "1111", |
| 90 | + "installationId" : "2222xyz" |
| 91 | + }`), |
| 92 | + decryptedJsonData: map[string]string{"privateKey": "foo"}, |
| 93 | + wantErr: errors.New("error parsing installation id"), |
| 94 | + }, |
| 95 | + } |
| 96 | + for _, tt := range tests { |
| 97 | + t.Run(tt.name, func(t *testing.T) { |
| 98 | + if tt.jsonData == nil { |
| 99 | + tt.jsonData = []byte(`{}`) |
| 100 | + } |
| 101 | + got, err := models.LoadSettings(backend.DataSourceInstanceSettings{JSONData: tt.jsonData, DecryptedSecureJSONData: tt.decryptedJsonData}) |
| 102 | + if tt.wantErr != nil { |
| 103 | + require.NotNil(t, err) |
| 104 | + assert.Equal(t, tt.wantErr.Error(), err.Error()) |
| 105 | + return |
| 106 | + } |
| 107 | + require.Nil(t, err) |
| 108 | + require.NotNil(t, got) |
| 109 | + assert.Equal(t, tt.want, got) |
| 110 | + }) |
| 111 | + } |
| 112 | +} |
0 commit comments