AuthorizationTests.coffee 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. expect = require("chai").expect
  2. async = require("async")
  3. User = require "./helpers/User"
  4. request = require "./helpers/request"
  5. settings = require "settings-sharelatex"
  6. MockDocstoreApi = require './helpers/MockDocstoreApi'
  7. MockDocUpdaterApi = require './helpers/MockDocUpdaterApi'
  8. try_read_access = (user, project_id, test, callback) ->
  9. async.series [
  10. (cb) ->
  11. user.request.get "/project/#{project_id}", (error, response, body) ->
  12. return cb(error) if error?
  13. test(response, body)
  14. cb()
  15. (cb) ->
  16. user.request.get "/project/#{project_id}/download/zip", (error, response, body) ->
  17. return cb(error) if error?
  18. test(response, body)
  19. cb()
  20. ], callback
  21. try_settings_write_access = (user, project_id, test, callback) ->
  22. async.series [
  23. (cb) ->
  24. user.request.post {
  25. uri: "/project/#{project_id}/settings"
  26. json:
  27. compiler: "latex"
  28. }, (error, response, body) ->
  29. return cb(error) if error?
  30. test(response, body)
  31. cb()
  32. ], callback
  33. try_admin_access = (user, project_id, test, callback) ->
  34. async.series [
  35. (cb) ->
  36. user.request.post {
  37. uri: "/project/#{project_id}/rename"
  38. json:
  39. newProjectName: "new-name"
  40. }, (error, response, body) ->
  41. return cb(error) if error?
  42. test(response, body)
  43. cb()
  44. (cb) ->
  45. user.request.post {
  46. uri: "/project/#{project_id}/settings/admin"
  47. json:
  48. publicAccessLevel: "private"
  49. }, (error, response, body) ->
  50. return cb(error) if error?
  51. test(response, body)
  52. cb()
  53. ], callback
  54. try_content_access = (user, project_id, test, callback) ->
  55. # The real-time service calls this end point to determine the user's
  56. # permissions.
  57. if user.id?
  58. user_id = user.id
  59. else
  60. user_id = "anonymous-user"
  61. request.post {
  62. url: "/project/#{project_id}/join"
  63. qs: {user_id}
  64. auth:
  65. user: settings.apis.web.user
  66. pass: settings.apis.web.pass
  67. sendImmediately: true
  68. json: true
  69. jar: false
  70. }, (error, response, body) ->
  71. return callback(error) if error?
  72. test(response, body)
  73. callback()
  74. expect_read_access = (user, project_id, callback) ->
  75. async.series [
  76. (cb) ->
  77. try_read_access(user, project_id, (response, body) ->
  78. expect(response.statusCode).to.be.oneOf [200, 204]
  79. , cb)
  80. (cb) ->
  81. try_content_access(user, project_id, (response, body) ->
  82. expect(body.privilegeLevel).to.be.oneOf ["owner", "readAndWrite", "readOnly"]
  83. , cb)
  84. ], callback
  85. expect_content_write_access = (user, project_id, callback) ->
  86. try_content_access(user, project_id, (response, body) ->
  87. expect(body.privilegeLevel).to.be.oneOf ["owner", "readAndWrite"]
  88. , callback)
  89. expect_settings_write_access = (user, project_id, callback) ->
  90. try_settings_write_access(user, project_id, (response, body) ->
  91. expect(response.statusCode).to.be.oneOf [200, 204]
  92. , callback)
  93. expect_admin_access = (user, project_id, callback) ->
  94. try_admin_access(user, project_id, (response, body) ->
  95. expect(response.statusCode).to.be.oneOf [200, 204]
  96. , callback)
  97. expect_no_read_access = (user, project_id, options, callback) ->
  98. async.series [
  99. (cb) ->
  100. try_read_access(user, project_id, (response, body) ->
  101. expect(response.statusCode).to.equal 302
  102. expect(response.headers.location).to.match new RegExp(options.redirect_to)
  103. , cb)
  104. (cb) ->
  105. try_content_access(user, project_id, (response, body) ->
  106. expect(body.privilegeLevel).to.be.equal false
  107. , cb)
  108. ], callback
  109. expect_no_content_write_access = (user, project_id, callback) ->
  110. try_content_access(user, project_id, (response, body) ->
  111. expect(body.privilegeLevel).to.be.oneOf [false, "readOnly"]
  112. , callback)
  113. expect_no_settings_write_access = (user, project_id, options, callback) ->
  114. try_settings_write_access(user, project_id, (response, body) ->
  115. expect(response.statusCode).to.equal 302
  116. expect(response.headers.location).to.match new RegExp(options.redirect_to)
  117. , callback)
  118. expect_no_admin_access = (user, project_id, options, callback) ->
  119. try_admin_access(user, project_id, (response, body) ->
  120. expect(response.statusCode).to.equal 302
  121. expect(response.headers.location).to.match new RegExp(options.redirect_to)
  122. , callback)
  123. describe "Authorization", ->
  124. before (done) ->
  125. @timeout(90000)
  126. @owner = new User()
  127. @other1 = new User()
  128. @other2 = new User()
  129. @anon = new User()
  130. @site_admin = new User({email: "admin@example.com"})
  131. async.parallel [
  132. (cb) => @owner.login cb
  133. (cb) => @other1.login cb
  134. (cb) => @other2.login cb
  135. (cb) => @anon.getCsrfToken cb
  136. (cb) =>
  137. @site_admin.login (err) =>
  138. return cb(err) if error?
  139. @site_admin.ensure_admin cb
  140. ], done
  141. describe "private project", ->
  142. before (done) ->
  143. @owner.createProject "private-project", (error, project_id) =>
  144. return done(error) if error?
  145. @project_id = project_id
  146. done()
  147. it "should allow the owner read access to it", (done) ->
  148. expect_read_access @owner, @project_id, done
  149. it "should allow the owner write access to its content", (done) ->
  150. expect_content_write_access @owner, @project_id, done
  151. it "should allow the owner write access to its settings", (done) ->
  152. expect_settings_write_access @owner, @project_id, done
  153. it "should allow the owner admin access to it", (done) ->
  154. expect_admin_access @owner, @project_id, done
  155. it "should not allow another user read access to the project", (done) ->
  156. expect_no_read_access @other1, @project_id, redirect_to: "/restricted", done
  157. it "should not allow another user write access to its content", (done) ->
  158. expect_no_content_write_access @other1, @project_id, done
  159. it "should not allow another user write access to its settings", (done) ->
  160. expect_no_settings_write_access @other1, @project_id, redirect_to: "/restricted", done
  161. it "should not allow another user admin access to it", (done) ->
  162. expect_no_admin_access @other1, @project_id, redirect_to: "/restricted", done
  163. it "should not allow anonymous user read access to it", (done) ->
  164. expect_no_read_access @anon, @project_id, redirect_to: "/restricted", done
  165. it "should not allow anonymous user write access to its content", (done) ->
  166. expect_no_content_write_access @anon, @project_id, done
  167. it "should not allow anonymous user write access to its settings", (done) ->
  168. expect_no_settings_write_access @anon, @project_id, redirect_to: "/restricted", done
  169. it "should not allow anonymous user admin access to it", (done) ->
  170. expect_no_admin_access @anon, @project_id, redirect_to: "/restricted", done
  171. it "should allow site admin users read access to it", (done) ->
  172. expect_read_access @site_admin, @project_id, done
  173. it "should allow site admin users write access to its content", (done) ->
  174. expect_content_write_access @site_admin, @project_id, done
  175. it "should allow site admin users write access to its settings", (done) ->
  176. expect_settings_write_access @site_admin, @project_id, done
  177. it "should allow site admin users admin access to it", (done) ->
  178. expect_admin_access @site_admin, @project_id, done
  179. describe "shared project", ->
  180. before (done) ->
  181. @rw_user = @other1
  182. @ro_user = @other2
  183. @owner.createProject "private-project", (error, project_id) =>
  184. return done(error) if error?
  185. @project_id = project_id
  186. @owner.addUserToProject @project_id, @ro_user, "readOnly", (error) =>
  187. return done(error) if error?
  188. @owner.addUserToProject @project_id, @rw_user, "readAndWrite", (error) =>
  189. return done(error) if error?
  190. done()
  191. it "should allow the read-only user read access to it", (done) ->
  192. expect_read_access @ro_user, @project_id, done
  193. it "should not allow the read-only user write access to its content", (done) ->
  194. expect_no_content_write_access @ro_user, @project_id, done
  195. it "should not allow the read-only user write access to its settings", (done) ->
  196. expect_no_settings_write_access @ro_user, @project_id, redirect_to: "/restricted", done
  197. it "should not allow the read-only user admin access to it", (done) ->
  198. expect_no_admin_access @ro_user, @project_id, redirect_to: "/restricted", done
  199. it "should allow the read-write user read access to it", (done) ->
  200. expect_read_access @rw_user, @project_id, done
  201. it "should allow the read-write user write access to its content", (done) ->
  202. expect_content_write_access @rw_user, @project_id, done
  203. it "should allow the read-write user write access to its settings", (done) ->
  204. expect_settings_write_access @rw_user, @project_id, done
  205. it "should not allow the read-write user admin access to it", (done) ->
  206. expect_no_admin_access @rw_user, @project_id, redirect_to: "/restricted", done
  207. describe "public read-write project", ->
  208. before (done) ->
  209. @owner.createProject "public-rw-project", (error, project_id) =>
  210. return done(error) if error?
  211. @project_id = project_id
  212. @owner.makePublic @project_id, "readAndWrite", done
  213. it "should allow a user read access to it", (done) ->
  214. expect_read_access @other1, @project_id, done
  215. it "should allow a user write access to its content", (done) ->
  216. expect_content_write_access @other1, @project_id, done
  217. it "should not allow a user write access to its settings", (done) ->
  218. expect_no_settings_write_access @other1, @project_id, redirect_to: "/restricted", done
  219. it "should not allow a user admin access to it", (done) ->
  220. expect_no_admin_access @other1, @project_id, redirect_to: "/restricted", done
  221. it "should allow an anonymous user read access to it", (done) ->
  222. expect_read_access @anon, @project_id, done
  223. it "should allow an anonymous user write access to its content", (done) ->
  224. expect_content_write_access @anon, @project_id, done
  225. it "should not allow an anonymous user write access to its settings", (done) ->
  226. expect_no_settings_write_access @anon, @project_id, redirect_to: "/restricted", done
  227. it "should not allow an anonymous user admin access to it", (done) ->
  228. expect_no_admin_access @anon, @project_id, redirect_to: "/restricted", done
  229. describe "public read-only project", ->
  230. before (done) ->
  231. @owner.createProject "public-ro-project", (error, project_id) =>
  232. return done(error) if error?
  233. @project_id = project_id
  234. @owner.makePublic @project_id, "readOnly", done
  235. it "should allow a user read access to it", (done) ->
  236. expect_read_access @other1, @project_id, done
  237. it "should not allow a user write access to its content", (done) ->
  238. expect_no_content_write_access @other1, @project_id, done
  239. it "should not allow a user write access to its settings", (done) ->
  240. expect_no_settings_write_access @other1, @project_id, redirect_to: "/restricted", done
  241. it "should not allow a user admin access to it", (done) ->
  242. expect_no_admin_access @other1, @project_id, redirect_to: "/restricted", done
  243. it "should allow an anonymous user read access to it", (done) ->
  244. expect_read_access @anon, @project_id, done
  245. it "should not allow an anonymous user write access to its content", (done) ->
  246. expect_no_content_write_access @anon, @project_id, done
  247. it "should not allow an anonymous user write access to its settings", (done) ->
  248. expect_no_settings_write_access @anon, @project_id, redirect_to: "/restricted", done
  249. it "should not allow an anonymous user admin access to it", (done) ->
  250. expect_no_admin_access @anon, @project_id, redirect_to: "/restricted", done