commands.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. Copyright (C) 2018 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. // @flow
  15. import config from '../config.js'
  16. const identityUrl = `${config.coriolisUrl}identity/auth/tokens`
  17. const projectsUrl = `${config.coriolisUrl}identity/auth/projects`
  18. const coriolisUrl = `${config.coriolisUrl}coriolis`
  19. Cypress.Commands.add('login', () => {
  20. let unscopedBody = {
  21. auth: {
  22. identity: {
  23. methods: ['password'],
  24. password: {
  25. user: {
  26. name: config.username,
  27. domain: { name: 'default' },
  28. password: config.password,
  29. },
  30. },
  31. },
  32. scope: 'unscoped',
  33. },
  34. }
  35. cy.request({
  36. method: 'POST',
  37. url: identityUrl,
  38. body: unscopedBody,
  39. }).then(unscopedResponse => {
  40. let unscopedToken = unscopedResponse.headers['x-subject-token']
  41. // $FlowIssue
  42. expect(unscopedToken).to.exist
  43. cy.request({
  44. method: 'GET',
  45. url: projectsUrl,
  46. headers: { 'X-Auth-Token': unscopedToken },
  47. }).then(projectsReponse => {
  48. let projects = projectsReponse.body.projects
  49. let cypressProject = projects.find(p => p.name === 'cypress')
  50. let projectId = cypressProject ? cypressProject.id : projects[0].id
  51. // $FlowIssue
  52. expect(projectId).to.exist
  53. let scopedBody = {
  54. auth: {
  55. identity: {
  56. methods: ['token'],
  57. token: {
  58. id: unscopedToken,
  59. },
  60. },
  61. scope: {
  62. project: {
  63. id: projectId,
  64. },
  65. },
  66. },
  67. }
  68. cy.request({
  69. method: 'POST',
  70. url: identityUrl,
  71. body: scopedBody,
  72. }).then(scopedResponse => {
  73. let scopedToken = scopedResponse.headers['x-subject-token']
  74. // $FlowIssue
  75. expect(scopedToken).to.exist
  76. cy.setCookie('token', scopedToken)
  77. cy.setCookie('projectId', projectId)
  78. cy.visit(config.nodeServer)
  79. })
  80. })
  81. })
  82. })
  83. Cypress.Commands.add('logout', () => {
  84. let token
  85. return cy.getCookies().then(cookies => {
  86. let tokenCookie = cookies.find(c => c.name === 'token')
  87. if (tokenCookie) {
  88. token = tokenCookie.value
  89. }
  90. }).then(() => {
  91. if (!token) {
  92. return Promise.resolve()
  93. }
  94. return cy.request({
  95. method: 'DELETE',
  96. url: `${config.coriolisUrl}identity/auth/tokens`,
  97. headers: { 'X-Subject-Token': token, 'X-Auth-Token': token },
  98. })
  99. })
  100. })
  101. Cypress.Commands.add('cleanup', () => {
  102. if (config.username !== 'cypress') {
  103. return Promise.resolve()
  104. }
  105. let token
  106. let projectId
  107. return cy.getCookies().then(cookies => {
  108. token = cookies.find(c => c.name === 'token').value
  109. projectId = cookies.find(c => c.name === 'projectId').value
  110. }).then(() => {
  111. if (!token || !projectId) {
  112. return Promise.resolve()
  113. }
  114. // Delete replicas
  115. return cy.request({
  116. method: 'GET',
  117. url: `${coriolisUrl}/${projectId}/replicas/detail`,
  118. headers: { 'X-Auth-Token': token },
  119. }).then(response => response.body.replicas)
  120. .then(replicas => Promise.all(replicas.map(replica => cy.request({
  121. method: 'DELETE',
  122. url: `${coriolisUrl}/${projectId}/replicas/${replica.id}`,
  123. headers: { 'X-Auth-Token': token },
  124. }))))
  125. }).then(() => {
  126. // Delete migrations
  127. return cy.request({
  128. method: 'GET',
  129. url: `${coriolisUrl}/${projectId}/migrations/detail`,
  130. headers: { 'X-Auth-Token': token },
  131. }).then(response => response.body.migrations)
  132. .then(migrations => Promise.all(migrations.map(migration => cy.request({
  133. method: 'DELETE',
  134. url: `${coriolisUrl}/${projectId}/migrations/${migration.id}`,
  135. headers: { 'X-Auth-Token': token },
  136. }))))
  137. }).then(() => {
  138. // Delete endpoints
  139. return cy.request({
  140. method: 'GET',
  141. url: `${coriolisUrl}/${projectId}/endpoints`,
  142. headers: { 'X-Auth-Token': token },
  143. }).then(response => response.body.endpoints)
  144. .then(endpoints => Promise.all(endpoints.map(endpoint => cy.request({
  145. method: 'DELETE',
  146. url: `${coriolisUrl}/${projectId}/endpoints/${endpoint.id}`,
  147. headers: { 'X-Auth-Token': token },
  148. }))))
  149. }).then(() => {
  150. // Delete users created by Cypress
  151. return cy.request({
  152. method: 'GET',
  153. url: `${config.coriolisUrl}identity/users`,
  154. headers: { 'X-Auth-Token': token },
  155. }).then(response => response.body.users
  156. .filter(user => user.description && /user created by cypress/gi.test(user.description))
  157. ).then(users => Promise.all(users.map(user => cy.request({
  158. method: 'DELETE',
  159. url: `${config.coriolisUrl}identity/users/${user.id}`,
  160. headers: { 'X-Auth-Token': token },
  161. }))))
  162. }).then(() => {
  163. // Delete projects created by Cypress
  164. return cy.request({
  165. method: 'GET',
  166. url: `${config.coriolisUrl}identity/projects`,
  167. headers: { 'X-Auth-Token': token },
  168. }).then(response => response.body.projects
  169. .filter(project => project.description && /project created by cypress/gi.test(project.description))
  170. ).then(projects => Promise.all(projects.map(project => cy.request({
  171. method: 'DELETE',
  172. url: `${config.coriolisUrl}identity/projects/${project.id}`,
  173. headers: { 'X-Auth-Token': token },
  174. }))))
  175. })
  176. })
  177. Cypress.Commands.add('getById', (id, type) => {
  178. return cy.get(`${type || ''}[data-test-id="${id}"]`)
  179. })
  180. Cypress.Commands.add('getServerConfig', () => {
  181. return cy.request({
  182. url: `${config.nodeServer}config`,
  183. method: 'GET',
  184. }).then(response => {
  185. return response.body
  186. })
  187. })