/engine/api/application_deployment_test.go

https://github.com/ovh/cds · Go · 371 lines · 301 code · 51 blank · 19 comment · 3 complexity · 0385ac122f38121b2c3f95740be356b0 MD5 · raw file

  1. package api
  2. import (
  3. "context"
  4. "encoding/json"
  5. "net/http"
  6. "net/http/httptest"
  7. "testing"
  8. "github.com/ovh/cds/engine/api/authentication"
  9. "github.com/stretchr/testify/require"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/ovh/cds/engine/api/application"
  12. "github.com/ovh/cds/engine/api/authentication/builtin"
  13. "github.com/ovh/cds/engine/api/integration"
  14. "github.com/ovh/cds/engine/api/test"
  15. "github.com/ovh/cds/engine/api/test/assets"
  16. "github.com/ovh/cds/sdk"
  17. "github.com/ovh/cds/sdk/cdsclient"
  18. )
  19. func Test_getApplicationDeploymentStrategiesConfigHandler(t *testing.T) {
  20. api, db, router := newTestAPI(t)
  21. u, pass := assets.InsertAdminUser(t, db)
  22. pkey := sdk.RandomString(10)
  23. proj := assets.InsertTestProject(t, db, api.Cache, pkey, pkey)
  24. app := &sdk.Application{
  25. Name: sdk.RandomString(10),
  26. }
  27. test.NoError(t, application.Insert(db, *proj, app))
  28. vars := map[string]string{
  29. "permProjectKey": proj.Key,
  30. "applicationName": app.Name,
  31. }
  32. uri := router.GetRoute("GET", api.getApplicationDeploymentStrategiesConfigHandler, vars)
  33. req, err := http.NewRequest("GET", uri, nil)
  34. test.NoError(t, err)
  35. assets.AuthentifyRequest(t, req, u, pass)
  36. // Do the request
  37. w := httptest.NewRecorder()
  38. router.Mux.ServeHTTP(w, req)
  39. assert.Equal(t, 200, w.Code)
  40. }
  41. func Test_postApplicationDeploymentStrategyConfigHandler(t *testing.T) {
  42. api, db, router := newTestAPI(t)
  43. u, pass := assets.InsertAdminUser(t, db)
  44. pkey := sdk.RandomString(10)
  45. proj := assets.InsertTestProject(t, db, api.Cache, pkey, pkey)
  46. app := &sdk.Application{
  47. Name: sdk.RandomString(10),
  48. }
  49. test.NoError(t, application.Insert(db, *proj, app))
  50. pf := sdk.IntegrationModel{
  51. Name: "test-deploy-post-2" + pkey,
  52. Deployment: true,
  53. }
  54. test.NoError(t, integration.InsertModel(db, &pf))
  55. defer func() { _ = integration.DeleteModel(db, pf.ID) }()
  56. pp := sdk.ProjectIntegration{
  57. Model: pf,
  58. Name: pf.Name,
  59. IntegrationModelID: pf.ID,
  60. ProjectID: proj.ID,
  61. Config: sdk.IntegrationConfig{
  62. "token": sdk.IntegrationConfigValue{
  63. Type: sdk.IntegrationConfigTypePassword,
  64. Value: "my-secret-token",
  65. },
  66. "url": sdk.IntegrationConfigValue{
  67. Type: sdk.IntegrationConfigTypeString,
  68. Value: "my-url",
  69. },
  70. },
  71. }
  72. test.NoError(t, integration.InsertIntegration(db, &pp))
  73. vars := map[string]string{
  74. "permProjectKey": proj.Key,
  75. "applicationName": app.Name,
  76. "integration": pf.Name,
  77. }
  78. uri := router.GetRoute("POST", api.postApplicationDeploymentStrategyConfigHandler, vars)
  79. req := assets.NewAuthentifiedRequest(t, u, pass, "POST", uri, sdk.IntegrationConfig{
  80. "token": sdk.IntegrationConfigValue{
  81. Type: sdk.IntegrationConfigTypePassword,
  82. Value: "my-secret-token",
  83. },
  84. "url": sdk.IntegrationConfigValue{
  85. Type: sdk.IntegrationConfigTypeString,
  86. Value: "my-url",
  87. },
  88. })
  89. // Do the request
  90. w := httptest.NewRecorder()
  91. router.Mux.ServeHTTP(w, req)
  92. assert.Equal(t, 200, w.Code)
  93. //Then we try to update
  94. req = assets.NewAuthentifiedRequest(t, u, pass, "POST", uri, sdk.IntegrationConfig{
  95. "token": sdk.IntegrationConfigValue{
  96. Type: sdk.IntegrationConfigTypePassword,
  97. Value: "my-secret-token-2",
  98. },
  99. "url": sdk.IntegrationConfigValue{
  100. Type: sdk.IntegrationConfigTypeString,
  101. Value: "my-url-2",
  102. },
  103. })
  104. // Do the request
  105. w = httptest.NewRecorder()
  106. router.Mux.ServeHTTP(w, req)
  107. assert.Equal(t, 200, w.Code)
  108. uri = router.GetRoute("GET", api.getApplicationDeploymentStrategyConfigHandler, vars)
  109. //Then we try to update
  110. req = assets.NewAuthentifiedRequest(t, u, pass, "GET", uri, nil)
  111. // Do the request
  112. w = httptest.NewRecorder()
  113. router.Mux.ServeHTTP(w, req)
  114. assert.Equal(t, 200, w.Code)
  115. cfg := sdk.IntegrationConfig{}
  116. test.NoError(t, json.Unmarshal(w.Body.Bytes(), &cfg))
  117. assert.Equal(t, sdk.PasswordPlaceholder, cfg["token"].Value)
  118. // with clear paswword
  119. uri = router.GetRoute("GET", api.getApplicationDeploymentStrategyConfigHandler, vars)
  120. // Then we try to update
  121. req = assets.NewAuthentifiedRequest(t, u, pass, "GET", uri, nil)
  122. q := req.URL.Query()
  123. q.Set("withClearPassword", "true")
  124. req.URL.RawQuery = q.Encode()
  125. // Do the request
  126. w = httptest.NewRecorder()
  127. router.Mux.ServeHTTP(w, req)
  128. assert.Equal(t, 200, w.Code)
  129. cfg2 := sdk.IntegrationConfig{}
  130. test.NoError(t, json.Unmarshal(w.Body.Bytes(), &cfg2))
  131. assert.Equal(t, "my-secret-token-2", cfg2["token"].Value)
  132. // with clear paswword
  133. uri = router.GetRoute("DELETE", api.deleteApplicationDeploymentStrategyConfigHandler, vars)
  134. // Then we try to update
  135. req = assets.NewAuthentifiedRequest(t, u, pass, "DELETE", uri, nil)
  136. // Do the request
  137. w = httptest.NewRecorder()
  138. router.Mux.ServeHTTP(w, req)
  139. assert.Equal(t, 200, w.Code)
  140. uri = router.GetRoute("GET", api.getApplicationDeploymentStrategyConfigHandler, vars)
  141. //Then we try to update
  142. req = assets.NewAuthentifiedRequest(t, u, pass, "GET", uri, nil)
  143. // Do the request
  144. w = httptest.NewRecorder()
  145. router.Mux.ServeHTTP(w, req)
  146. assert.Equal(t, 404, w.Code)
  147. }
  148. func Test_postApplicationDeploymentStrategyConfigHandler_InsertTwoDifferentIntegrations(t *testing.T) {
  149. api, db, router := newTestAPI(t)
  150. u, pass := assets.InsertAdminUser(t, db)
  151. pkey := sdk.RandomString(10)
  152. proj := assets.InsertTestProject(t, db, api.Cache, pkey, pkey)
  153. app := &sdk.Application{
  154. Name: sdk.RandomString(10),
  155. }
  156. test.NoError(t, application.Insert(db, *proj, app))
  157. pf := sdk.IntegrationModel{
  158. Name: "test-deploy-TwoDifferentIntegrations-2" + pkey,
  159. Deployment: true,
  160. }
  161. test.NoError(t, integration.InsertModel(db, &pf))
  162. defer func() { _ = integration.DeleteModel(db, pf.ID) }()
  163. pp := sdk.ProjectIntegration{
  164. Model: pf,
  165. Name: pf.Name,
  166. IntegrationModelID: pf.ID,
  167. ProjectID: proj.ID,
  168. Config: sdk.IntegrationConfig{
  169. "token": sdk.IntegrationConfigValue{
  170. Type: sdk.IntegrationConfigTypePassword,
  171. Value: "my-secret-token",
  172. },
  173. "url": sdk.IntegrationConfigValue{
  174. Type: sdk.IntegrationConfigTypeString,
  175. Value: "my-url",
  176. },
  177. },
  178. }
  179. test.NoError(t, integration.InsertIntegration(db, &pp))
  180. pp2 := sdk.ProjectIntegration{
  181. Model: pf,
  182. Name: pf.Name + "-2",
  183. IntegrationModelID: pf.ID,
  184. ProjectID: proj.ID,
  185. Config: sdk.IntegrationConfig{
  186. "token": sdk.IntegrationConfigValue{
  187. Type: sdk.IntegrationConfigTypePassword,
  188. Value: "my-secret-token",
  189. },
  190. "url": sdk.IntegrationConfigValue{
  191. Type: sdk.IntegrationConfigTypeString,
  192. Value: "my-url",
  193. },
  194. },
  195. }
  196. test.NoError(t, integration.InsertIntegration(db, &pp2))
  197. vars := map[string]string{
  198. "permProjectKey": proj.Key,
  199. "applicationName": app.Name,
  200. "integration": pf.Name,
  201. }
  202. uri := router.GetRoute("POST", api.postApplicationDeploymentStrategyConfigHandler, vars)
  203. req := assets.NewAuthentifiedRequest(t, u, pass, "POST", uri, sdk.IntegrationConfig{
  204. "token": sdk.IntegrationConfigValue{
  205. Type: sdk.IntegrationConfigTypePassword,
  206. Value: "my-secret-token",
  207. },
  208. "url": sdk.IntegrationConfigValue{
  209. Type: sdk.IntegrationConfigTypeString,
  210. Value: "my-url",
  211. },
  212. })
  213. // Do the request
  214. w := httptest.NewRecorder()
  215. router.Mux.ServeHTTP(w, req)
  216. assert.Equal(t, 200, w.Code)
  217. //Now add a new
  218. vars = map[string]string{
  219. "permProjectKey": proj.Key,
  220. "applicationName": app.Name,
  221. "integration": pp2.Name,
  222. }
  223. uri = router.GetRoute("POST", api.postApplicationDeploymentStrategyConfigHandler, vars)
  224. req = assets.NewAuthentifiedRequest(t, u, pass, "POST", uri, sdk.IntegrationConfig{
  225. "token": sdk.IntegrationConfigValue{
  226. Type: sdk.IntegrationConfigTypePassword,
  227. Value: "my-secret-token",
  228. },
  229. "url": sdk.IntegrationConfigValue{
  230. Type: sdk.IntegrationConfigTypeString,
  231. Value: "my-url",
  232. },
  233. })
  234. // Do the request
  235. w = httptest.NewRecorder()
  236. router.Mux.ServeHTTP(w, req)
  237. assert.Equal(t, 200, w.Code)
  238. vars = map[string]string{
  239. "permProjectKey": proj.Key,
  240. "applicationName": app.Name,
  241. }
  242. uri = router.GetRoute("GET", api.getApplicationDeploymentStrategiesConfigHandler, vars)
  243. //Then we try to update
  244. req = assets.NewAuthentifiedRequest(t, u, pass, "GET", uri, nil)
  245. // Do the request
  246. w = httptest.NewRecorder()
  247. router.Mux.ServeHTTP(w, req)
  248. assert.Equal(t, 200, w.Code)
  249. cfg := map[string]sdk.IntegrationConfig{}
  250. test.NoError(t, json.Unmarshal(w.Body.Bytes(), &cfg))
  251. assert.Len(t, cfg, 2)
  252. }
  253. func Test_postApplicationDeploymentStrategyConfigHandlerAsProvider(t *testing.T) {
  254. api, db, tsURL := newTestServer(t)
  255. u, _ := assets.InsertAdminUser(t, db)
  256. localConsumer, err := authentication.LoadConsumerByTypeAndUserID(context.TODO(), api.mustDB(), sdk.ConsumerLocal, u.ID, authentication.LoadConsumerOptions.WithAuthentifiedUser)
  257. require.NoError(t, err)
  258. _, jws, err := builtin.NewConsumer(context.TODO(), db, sdk.RandomString(10), sdk.RandomString(10), localConsumer, u.GetGroupIDs(),
  259. sdk.NewAuthConsumerScopeDetails(sdk.AuthConsumerScopeProject))
  260. pkey := sdk.RandomString(10)
  261. proj := assets.InsertTestProject(t, db, api.Cache, pkey, pkey)
  262. app := &sdk.Application{
  263. Name: sdk.RandomString(10),
  264. }
  265. test.NoError(t, application.Insert(db, *proj, app))
  266. pf := sdk.IntegrationModel{
  267. Name: "test-deploy-3" + pkey,
  268. Deployment: true,
  269. DeploymentDefaultConfig: sdk.IntegrationConfig{
  270. "token": sdk.IntegrationConfigValue{
  271. Type: sdk.IntegrationConfigTypePassword,
  272. Value: "my-secret-token",
  273. },
  274. "url": sdk.IntegrationConfigValue{
  275. Type: sdk.IntegrationConfigTypeString,
  276. Value: "my-url",
  277. },
  278. },
  279. }
  280. test.NoError(t, integration.InsertModel(db, &pf))
  281. defer func() { _ = integration.DeleteModel(api.mustDB(), pf.ID) }()
  282. pp := sdk.ProjectIntegration{
  283. Model: pf,
  284. Name: pf.Name,
  285. IntegrationModelID: pf.ID,
  286. ProjectID: proj.ID,
  287. }
  288. test.NoError(t, integration.InsertIntegration(db, &pp))
  289. sdkclient := cdsclient.NewProviderClient(cdsclient.ProviderConfig{
  290. Host: tsURL,
  291. Token: jws,
  292. })
  293. err = sdkclient.ApplicationDeploymentStrategyUpdate(proj.Key, app.Name, pf.Name, sdk.IntegrationConfig{
  294. "token": sdk.IntegrationConfigValue{
  295. Type: sdk.IntegrationConfigTypePassword,
  296. Value: "my-secret-token-2",
  297. },
  298. })
  299. test.NoError(t, err)
  300. cfg, err := application.LoadDeploymentStrategies(api.mustDB(), app.ID, true)
  301. test.NoError(t, err)
  302. var assertCfg = func(key string, cfg sdk.IntegrationConfig, expected sdk.IntegrationConfigValue) {
  303. actual, has := cfg[key]
  304. assert.True(t, has, "%s not found", key)
  305. assert.Equal(t, expected.Value, actual.Value)
  306. assert.Equal(t, expected.Type, actual.Type)
  307. }
  308. pfcfg, has := cfg[pf.Name]
  309. assert.True(t, has, "%s not found", pf.Name)
  310. assertCfg("token", pfcfg,
  311. sdk.IntegrationConfigValue{
  312. Type: sdk.IntegrationConfigTypePassword,
  313. Value: "my-secret-token-2",
  314. })
  315. assertCfg("url", pfcfg,
  316. sdk.IntegrationConfigValue{
  317. Type: sdk.IntegrationConfigTypeString,
  318. Value: "my-url",
  319. })
  320. }