AuthorizationTests.coffee 11 KB

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