git-bridge.spec.ts 12 KB

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