git-bridge.spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. import { v4 as uuid } from 'uuid'
  2. import { isExcludedBySharding, startWith } from './helpers/config'
  3. import { ensureUserExists, login } from './helpers/login'
  4. import {
  5. createProject,
  6. enableLinkSharing,
  7. shareProjectByEmailAndAcceptInviteViaDash,
  8. } from './helpers/project'
  9. import git from 'isomorphic-git'
  10. import http from 'isomorphic-git/http/web'
  11. import LightningFS from '@isomorphic-git/lightning-fs'
  12. import { throttledRecompile } from './helpers/compile'
  13. describe('git-bridge', function () {
  14. const ENABLED_VARS = {
  15. GIT_BRIDGE_ENABLED: 'true',
  16. GIT_BRIDGE_HOST: 'git-bridge',
  17. GIT_BRIDGE_PORT: '8000',
  18. V1_HISTORY_URL: 'http://sharelatex:3100/api',
  19. }
  20. const gitBridgePublicHost =
  21. Cypress.env('GIT_BRIDGE_PUBLIC_HOST') || 'sharelatex'
  22. describe('enabled in Server Pro', function () {
  23. if (isExcludedBySharding('PRO_CUSTOM_1')) return
  24. startWith({
  25. pro: true,
  26. vars: ENABLED_VARS,
  27. })
  28. ensureUserExists({ email: 'user@example.com' })
  29. function clearAllTokens() {
  30. cy.get('button.linking-git-bridge-revoke-button').each(el => {
  31. cy.wrap(el).click()
  32. cy.findByText('Delete token').click()
  33. })
  34. }
  35. function maybeClearAllTokens() {
  36. cy.visit('/user/settings')
  37. cy.findByText('Git Integration')
  38. cy.get('button')
  39. .contains(/Generate token|Add another token/)
  40. .then(btn => {
  41. if (btn.text() === 'Add another token') {
  42. clearAllTokens()
  43. }
  44. })
  45. }
  46. beforeEach(function () {
  47. login('user@example.com')
  48. })
  49. it('should render the git-bridge UI in the settings', () => {
  50. maybeClearAllTokens()
  51. cy.visit('/user/settings')
  52. cy.findByText('Git Integration')
  53. cy.get('button').contains('Generate token').click()
  54. cy.get('code')
  55. .contains(/olp_[a-zA-Z0-9]{16}/)
  56. .as('newToken')
  57. cy.findAllByText('Close').last().click()
  58. cy.get('@newToken').then(token => {
  59. // There can be more than one token with the same prefix when retrying
  60. cy.findAllByText(
  61. `${token.text().slice(0, 'olp_1234'.length)}${'*'.repeat(12)}`
  62. ).should('have.length.at.least', 1)
  63. })
  64. cy.get('button').contains('Generate token').should('not.exist')
  65. cy.get('button').contains('Add another token').should('exist')
  66. clearAllTokens()
  67. cy.get('button').contains('Generate token').should('exist')
  68. cy.get('button').contains('Add another token').should('not.exist')
  69. })
  70. it('should render the git-bridge UI in the editor', function () {
  71. maybeClearAllTokens()
  72. cy.visit('/project')
  73. createProject('git').as('projectId')
  74. cy.get('header').findByText('Menu').click()
  75. cy.findByText('Sync')
  76. cy.findByText('Git').click()
  77. cy.findByRole('dialog').within(() => {
  78. cy.get('@projectId').then(id => {
  79. cy.get('code').contains(
  80. `git clone http://git@${gitBridgePublicHost}/git/${id}`
  81. )
  82. })
  83. cy.findByRole('button', {
  84. name: 'Generate token',
  85. }).click()
  86. cy.get('code').contains(/olp_[a-zA-Z0-9]{16}/)
  87. })
  88. // Re-open
  89. cy.url().then(url => cy.visit(url))
  90. cy.get('header').findByText('Menu').click()
  91. cy.findByText('Git').click()
  92. cy.findByRole('dialog').within(() => {
  93. cy.get('@projectId').then(id => {
  94. cy.get('code').contains(
  95. `git clone http://git@${gitBridgePublicHost}/git/${id}`
  96. )
  97. })
  98. cy.findByText('Generate token').should('not.exist')
  99. cy.findByText(/generate a new one in Account Settings/)
  100. cy.findByText('Go to settings')
  101. .should('have.attr', 'target', '_blank')
  102. .and('have.attr', 'href', '/user/settings')
  103. })
  104. })
  105. describe('git access', () => {
  106. ensureUserExists({ email: 'collaborator-rw@example.com' })
  107. ensureUserExists({ email: 'collaborator-ro@example.com' })
  108. ensureUserExists({ email: 'collaborator-link-rw@example.com' })
  109. ensureUserExists({ email: 'collaborator-link-ro@example.com' })
  110. let projectName: string
  111. beforeEach(() => {
  112. cy.visit('/project')
  113. projectName = uuid()
  114. createProject(projectName).as('projectId')
  115. })
  116. it('should expose r/w interface to owner', () => {
  117. maybeClearAllTokens()
  118. cy.visit('/project')
  119. cy.findByText(projectName).click()
  120. checkGitAccess('readAndWrite')
  121. })
  122. it('should expose r/w interface to invited r/w collaborator', () => {
  123. shareProjectByEmailAndAcceptInviteViaDash(
  124. projectName,
  125. 'collaborator-rw@example.com',
  126. 'Can edit'
  127. )
  128. maybeClearAllTokens()
  129. cy.visit('/project')
  130. cy.findByText(projectName).click()
  131. checkGitAccess('readAndWrite')
  132. })
  133. it('should expose r/o interface to invited r/o collaborator', () => {
  134. shareProjectByEmailAndAcceptInviteViaDash(
  135. projectName,
  136. 'collaborator-ro@example.com',
  137. 'Read only'
  138. )
  139. maybeClearAllTokens()
  140. cy.visit('/project')
  141. cy.findByText(projectName).click()
  142. checkGitAccess('readOnly')
  143. })
  144. it('should expose r/w interface to link-sharing r/w collaborator', () => {
  145. enableLinkSharing().then(({ linkSharingReadAndWrite }) => {
  146. login('collaborator-link-rw@example.com')
  147. maybeClearAllTokens()
  148. cy.visit(linkSharingReadAndWrite)
  149. cy.findByText(projectName) // wait for lazy loading
  150. cy.findByText('Join Project').click()
  151. checkGitAccess('readAndWrite')
  152. })
  153. })
  154. it('should expose r/o interface to link-sharing r/o collaborator', () => {
  155. enableLinkSharing().then(({ linkSharingReadOnly }) => {
  156. login('collaborator-link-ro@example.com')
  157. maybeClearAllTokens()
  158. cy.visit(linkSharingReadOnly)
  159. cy.findByText(projectName) // wait for lazy loading
  160. cy.findByText('Join Project').click()
  161. checkGitAccess('readOnly')
  162. })
  163. })
  164. })
  165. function checkGitAccess(access: 'readOnly' | 'readAndWrite') {
  166. const recompile = throttledRecompile()
  167. cy.get('header').findByText('Menu').click()
  168. cy.findByText('Sync')
  169. cy.findByText('Git').click()
  170. cy.get('@projectId').then(projectId => {
  171. cy.findByRole('dialog').within(() => {
  172. cy.get('code').contains(
  173. `git clone http://git@${gitBridgePublicHost}/git/${projectId}`
  174. )
  175. })
  176. cy.findByRole('button', {
  177. name: 'Generate token',
  178. }).click()
  179. cy.get('code')
  180. .contains(/olp_[a-zA-Z0-9]{16}/)
  181. .then(async tokenEl => {
  182. const token = tokenEl.text()
  183. // close Git modal
  184. cy.findAllByText('Close').last().click()
  185. // close editor menu
  186. cy.get('#left-menu-modal').click()
  187. const fs = new LightningFS('fs')
  188. const dir = `/${projectId}`
  189. async function readFile(path: string): Promise<string> {
  190. return new Promise((resolve, reject) => {
  191. fs.readFile(path, { encoding: 'utf8' }, (err, blob) => {
  192. if (err) return reject(err)
  193. resolve(blob as string)
  194. })
  195. })
  196. }
  197. async function writeFile(path: string, data: string) {
  198. return new Promise<void>((resolve, reject) => {
  199. fs.writeFile(path, data, undefined, err => {
  200. if (err) return reject(err)
  201. resolve()
  202. })
  203. })
  204. }
  205. const commonOptions = {
  206. dir,
  207. fs,
  208. }
  209. const httpOptions = {
  210. http,
  211. url: `http://sharelatex/git/${projectId}`,
  212. headers: {
  213. Authorization: `Basic ${Buffer.from(`git:${token}`).toString('base64')}`,
  214. },
  215. }
  216. const authorOptions = {
  217. author: { name: 'user', email: 'user@example.com' },
  218. committer: { name: 'user', email: 'user@example.com' },
  219. }
  220. const mainTex = `${dir}/main.tex`
  221. // Clone
  222. cy.then({ timeout: 10_000 }, async () => {
  223. await git.clone({
  224. ...commonOptions,
  225. ...httpOptions,
  226. })
  227. })
  228. cy.findByText(/\\documentclass/)
  229. .parent()
  230. .parent()
  231. .then(async editor => {
  232. const onDisk = await readFile(mainTex)
  233. expect(onDisk.replaceAll('\n', '')).to.equal(editor.text())
  234. })
  235. const text = `
  236. \\documentclass{article}
  237. \\begin{document}
  238. Hello world
  239. \\end{document}
  240. `
  241. // Make a change
  242. cy.then(async () => {
  243. await writeFile(mainTex, text)
  244. await git.add({
  245. ...commonOptions,
  246. filepath: 'main.tex',
  247. })
  248. await git.commit({
  249. ...commonOptions,
  250. ...authorOptions,
  251. message: 'Swap main.tex',
  252. })
  253. })
  254. if (access === 'readAndWrite') {
  255. // check history before push
  256. cy.findAllByText('History').last().click()
  257. cy.findByText('(via Git)').should('not.exist')
  258. cy.findAllByText('Back to editor').last().click()
  259. cy.then(async () => {
  260. await git.push({
  261. ...commonOptions,
  262. ...httpOptions,
  263. })
  264. })
  265. } else {
  266. cy.then(async () => {
  267. try {
  268. await git.push({
  269. ...commonOptions,
  270. ...httpOptions,
  271. })
  272. expect.fail('push should have failed')
  273. } catch (err) {
  274. expect(err).to.match(/branches were not updated/)
  275. expect(err).to.match(/forbidden/)
  276. }
  277. })
  278. return // return early, below are write access bits
  279. }
  280. // check push in editor
  281. cy.findByText(/\\documentclass/)
  282. .parent()
  283. .parent()
  284. .should('have.text', text.replaceAll('\n', ''))
  285. // Wait for history sync - trigger flush by toggling the UI
  286. cy.findAllByText('History').last().click()
  287. cy.findAllByText('Back to editor').last().click()
  288. // check push in history
  289. cy.findAllByText('History').last().click()
  290. cy.findByText(/Hello world/)
  291. cy.findByText('(via Git)').should('exist')
  292. // Back to the editor
  293. cy.findAllByText('Back to editor').last().click()
  294. cy.findByText(/\\documentclass/)
  295. .parent()
  296. .parent()
  297. .click()
  298. .type('% via editor{enter}')
  299. // Trigger flush via compile
  300. recompile()
  301. // Back into the history, check what we just added
  302. cy.findAllByText('History').last().click()
  303. cy.findByText(/% via editor/)
  304. // Pull the change
  305. cy.then(async () => {
  306. await git.pull({
  307. ...commonOptions,
  308. ...httpOptions,
  309. ...authorOptions,
  310. })
  311. expect(await readFile(mainTex)).to.equal(text + '% via editor\n')
  312. })
  313. })
  314. })
  315. }
  316. })
  317. function checkDisabled() {
  318. ensureUserExists({ email: 'user@example.com' })
  319. it('should not render the git-bridge UI in the settings', () => {
  320. login('user@example.com')
  321. cy.visit('/user/settings')
  322. cy.findByText('Git Integration').should('not.exist')
  323. })
  324. it('should not render the git-bridge UI in the editor', function () {
  325. login('user@example.com')
  326. cy.visit('/project')
  327. createProject('maybe git')
  328. cy.get('header').findByText('Menu').click()
  329. cy.findByText('Word Count') // wait for lazy loading
  330. cy.findByText('Sync').should('not.exist')
  331. cy.findByText('Git').should('not.exist')
  332. })
  333. }
  334. describe('disabled in Server Pro', () => {
  335. if (isExcludedBySharding('PRO_DEFAULT_1')) return
  336. startWith({
  337. pro: true,
  338. })
  339. checkDisabled()
  340. })
  341. describe('unavailable in CE', () => {
  342. if (isExcludedBySharding('CE_CUSTOM_1')) return
  343. startWith({
  344. pro: false,
  345. vars: ENABLED_VARS,
  346. })
  347. checkDisabled()
  348. })
  349. })