|
| 1 | +package host |
| 2 | + |
| 3 | +import ( |
| 4 | + "regexp" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "npm/internal/model" |
| 9 | + "npm/internal/status" |
| 10 | + "npm/internal/test" |
| 11 | + "npm/internal/types" |
| 12 | + |
| 13 | + "github.com/DATA-DOG/go-sqlmock" |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | + "github.com/stretchr/testify/suite" |
| 17 | +) |
| 18 | + |
| 19 | +// +------------+ |
| 20 | +// | Setup | |
| 21 | +// +------------+ |
| 22 | + |
| 23 | +type testsuite struct { |
| 24 | + suite.Suite |
| 25 | + mock sqlmock.Sqlmock |
| 26 | + singleRow *sqlmock.Rows |
| 27 | +} |
| 28 | + |
| 29 | +func makeJSONB(str string) types.JSONB { |
| 30 | + m := types.JSONB{} |
| 31 | + m.UnmarshalJSON([]byte(str)) |
| 32 | + return m |
| 33 | +} |
| 34 | + |
| 35 | +// SetupTest is executed before each test |
| 36 | +func (s *testsuite) SetupTest() { |
| 37 | + var err error |
| 38 | + s.mock, err = test.Setup() |
| 39 | + require.NoError(s.T(), err) |
| 40 | + |
| 41 | + // These rows need to be intantiated for each test as they are |
| 42 | + // read in the db object, and their row position is not resettable |
| 43 | + // between tests. |
| 44 | + s.singleRow = sqlmock.NewRows([]string{ |
| 45 | + "id", |
| 46 | + "user_id", |
| 47 | + "type", |
| 48 | + "nginx_template_id", |
| 49 | + "listen_interface", |
| 50 | + "domain_names", |
| 51 | + "upstream_id", |
| 52 | + "proxy_scheme", |
| 53 | + "proxy_host", |
| 54 | + "proxy_port", |
| 55 | + "certificate_id", |
| 56 | + "access_list_id", |
| 57 | + "ssl_forced", |
| 58 | + "caching_enabled", |
| 59 | + "block_exploits", |
| 60 | + "allow_websocket_upgrade", |
| 61 | + "http2_support", |
| 62 | + "hsts_enabled", |
| 63 | + "hsts_subdomains", |
| 64 | + "paths", |
| 65 | + "advanced_config", |
| 66 | + "status", |
| 67 | + "error_message", |
| 68 | + "is_disabled", |
| 69 | + }).AddRow( |
| 70 | + 10, // ID |
| 71 | + 100, // UserID |
| 72 | + "proxy", // Type |
| 73 | + 20, // NginxTemplateID |
| 74 | + "", // ListenInterface |
| 75 | + makeJSONB("[\"example.com\"]"), // DomainNames |
| 76 | + 0, // UpstreamID |
| 77 | + "http", // ProxyScheme |
| 78 | + "127.0.0.1", // ProxyHost |
| 79 | + 3000, // ProxyPort |
| 80 | + types.NullableDBUint{Uint: 0}, // CertificateID |
| 81 | + types.NullableDBUint{Uint: 0}, // AccessListID |
| 82 | + false, // SSLForced |
| 83 | + false, // CachingEnabled |
| 84 | + false, // BlockExploits |
| 85 | + false, // AllowWebsocketUpgrade |
| 86 | + false, // HTTP2Support |
| 87 | + false, // HSTSEnabled |
| 88 | + false, // HSTSSubdomains |
| 89 | + "", // Paths |
| 90 | + "", // AdvancedConfig |
| 91 | + status.StatusReady, // Status |
| 92 | + "", // ErrorMessage |
| 93 | + false, // IsDisabled |
| 94 | + ) |
| 95 | +} |
| 96 | + |
| 97 | +// In order for 'go test' to run this suite, we need to create |
| 98 | +// a normal test function and pass our suite to suite.Run |
| 99 | +func TestExampleTestSuite(t *testing.T) { |
| 100 | + suite.Run(t, new(testsuite)) |
| 101 | +} |
| 102 | + |
| 103 | +// +------------+ |
| 104 | +// | Tests | |
| 105 | +// +------------+ |
| 106 | + |
| 107 | +func (s *testsuite) TestGetByID() { |
| 108 | + s.mock. |
| 109 | + ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "host" WHERE "host"."id" = $1 AND "host"."is_deleted" = $2 ORDER BY "host"."id" LIMIT 1`)). |
| 110 | + WithArgs(10, 0). |
| 111 | + WillReturnRows(s.singleRow) |
| 112 | + |
| 113 | + m, err := GetByID(10) |
| 114 | + require.NoError(s.T(), err) |
| 115 | + require.NoError(s.T(), s.mock.ExpectationsWereMet()) |
| 116 | + assert.Equal(s.T(), uint(10), m.ID) |
| 117 | +} |
| 118 | + |
| 119 | +func (s *testsuite) TestSave() { |
| 120 | + s.mock.ExpectBegin() |
| 121 | + s.mock.ExpectQuery(regexp.QuoteMeta(`INSERT INTO "host" ("created_at","updated_at","is_deleted","user_id","type","nginx_template_id","listen_interface","domain_names","upstream_id","proxy_scheme","proxy_host","proxy_port","certificate_id","access_list_id","ssl_forced","caching_enabled","block_exploits","allow_websocket_upgrade","http2_support","hsts_enabled","hsts_subdomains","paths","advanced_config","status","error_message","is_disabled") VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26) RETURNING "id"`)). |
| 122 | + WithArgs( |
| 123 | + sqlmock.AnyArg(), |
| 124 | + sqlmock.AnyArg(), |
| 125 | + 0, |
| 126 | + 100, |
| 127 | + "proxy", |
| 128 | + 20, |
| 129 | + "", |
| 130 | + "[\"example.com\"]", |
| 131 | + nil, |
| 132 | + "http", |
| 133 | + "127.0.0.1", |
| 134 | + 3000, // proxy_port |
| 135 | + nil, |
| 136 | + nil, |
| 137 | + false, |
| 138 | + false, |
| 139 | + false, |
| 140 | + false, |
| 141 | + false, |
| 142 | + false, |
| 143 | + false, |
| 144 | + "", |
| 145 | + "", |
| 146 | + status.StatusReady, |
| 147 | + "", |
| 148 | + false, |
| 149 | + ). |
| 150 | + WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow("11")) |
| 151 | + s.mock.ExpectCommit() |
| 152 | + |
| 153 | + // New model, as system |
| 154 | + m := Model{ |
| 155 | + UserID: 100, |
| 156 | + Type: "proxy", |
| 157 | + NginxTemplateID: 20, |
| 158 | + DomainNames: makeJSONB("[\"example.com\"]"), |
| 159 | + ProxyScheme: "http", |
| 160 | + ProxyHost: "127.0.0.1", |
| 161 | + ProxyPort: 3000, |
| 162 | + Status: status.StatusReady, |
| 163 | + } |
| 164 | + err := m.Save(true) |
| 165 | + require.NoError(s.T(), err) |
| 166 | + require.NoError(s.T(), s.mock.ExpectationsWereMet()) |
| 167 | +} |
| 168 | + |
| 169 | +func (s *testsuite) TestDelete() { |
| 170 | + s.mock.ExpectBegin() |
| 171 | + s.mock. |
| 172 | + ExpectExec(regexp.QuoteMeta(`UPDATE "host" SET "is_deleted"=$1 WHERE "host"."id" = $2 AND "host"."is_deleted" = $3`)). |
| 173 | + WithArgs(1, 10, 0). |
| 174 | + WillReturnResult(sqlmock.NewResult(0, 1)) |
| 175 | + s.mock.ExpectCommit() |
| 176 | + |
| 177 | + m := Model{} |
| 178 | + err := m.Delete() |
| 179 | + assert.Equal(s.T(), "Unable to delete a new object", err.Error()) |
| 180 | + |
| 181 | + m2 := Model{ |
| 182 | + ModelBase: model.ModelBase{ |
| 183 | + ID: 10, |
| 184 | + }, |
| 185 | + } |
| 186 | + err2 := m2.Delete() |
| 187 | + require.NoError(s.T(), err2) |
| 188 | + require.NoError(s.T(), s.mock.ExpectationsWereMet()) |
| 189 | +} |
| 190 | + |
| 191 | +func (s *testsuite) TestGetTemplate() { |
| 192 | + m := Model{ |
| 193 | + ModelBase: model.ModelBase{ |
| 194 | + ID: 10, |
| 195 | + CreatedAt: time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC).UnixMilli(), |
| 196 | + UpdatedAt: time.Date(2018, 8, 12, 7, 30, 24, 16, time.UTC).UnixMilli(), |
| 197 | + }, |
| 198 | + UserID: 100, |
| 199 | + Type: "proxy", |
| 200 | + NginxTemplateID: 20, |
| 201 | + DomainNames: makeJSONB("[\"example.com\"]"), |
| 202 | + ProxyScheme: "http", |
| 203 | + ProxyHost: "127.0.0.1", |
| 204 | + ProxyPort: 3000, |
| 205 | + Status: status.StatusReady, |
| 206 | + } |
| 207 | + |
| 208 | + t := m.GetTemplate() |
| 209 | + assert.Equal(s.T(), uint(10), t.ID) |
| 210 | + assert.Equal(s.T(), "Mon, 01 Jan 2018 10:00:00 AEST", t.CreatedAt) |
| 211 | + assert.Equal(s.T(), "Sun, 12 Aug 2018 17:30:24 AEST", t.UpdatedAt) |
| 212 | + assert.Equal(s.T(), uint(100), t.UserID) |
| 213 | + assert.Equal(s.T(), "proxy", t.Type) |
| 214 | + assert.Equal(s.T(), uint(20), t.NginxTemplateID) |
| 215 | + assert.Equal(s.T(), "http", t.ProxyScheme) |
| 216 | + assert.Equal(s.T(), "127.0.0.1", t.ProxyHost) |
| 217 | + assert.Equal(s.T(), 3000, t.ProxyPort) |
| 218 | + assert.Equal(s.T(), []string{"example.com"}, t.DomainNames) |
| 219 | + assert.Equal(s.T(), status.StatusReady, t.Status) |
| 220 | +} |
0 commit comments