/tests/backend/specs/api/sessionsAndGroups.js

https://gitlab.com/auchalet/etherpad-lite · JavaScript · 365 lines · 283 code · 39 blank · 43 comment · 51 complexity · 0c2d7e9429ea7a684dd2a402a0cdde27 MD5 · raw file

  1. var assert = require('assert')
  2. supertest = require(__dirname+'/../../../../src/node_modules/supertest'),
  3. fs = require('fs'),
  4. settings = require(__dirname+'/../../loadSettings').loadSettings(),
  5. api = supertest('http://'+settings.ip+":"+settings.port),
  6. path = require('path');
  7. var filePath = path.join(__dirname, '../../../../APIKEY.txt');
  8. var apiKey = fs.readFileSync(filePath, {encoding: 'utf-8'});
  9. apiKey = apiKey.replace(/\n$/, "");
  10. var apiVersion = 1;
  11. var testPadId = makeid();
  12. var groupID = "";
  13. var authorID = "";
  14. var sessionID = "";
  15. var padID = makeid();
  16. describe('API Versioning', function(){
  17. it('errors if can not connect', function(done) {
  18. api.get('/api/')
  19. .expect(function(res){
  20. apiVersion = res.body.currentVersion;
  21. if (!res.body.currentVersion) throw new Error("No version set in API");
  22. return;
  23. })
  24. .expect(200, done)
  25. });
  26. })
  27. // BEGIN GROUP AND AUTHOR TESTS
  28. /////////////////////////////////////
  29. /////////////////////////////////////
  30. /* Tests performed
  31. -> createGroup() -- should return a groupID
  32. -> listSessionsOfGroup(groupID) -- should be 0
  33. -> deleteGroup(groupID)
  34. -> createGroupIfNotExistsFor(groupMapper) -- should return a groupID
  35. -> createAuthor([name]) -- should return an authorID
  36. -> createAuthorIfNotExistsFor(authorMapper [, name]) -- should return an authorID
  37. -> getAuthorName(authorID) -- should return a name IE "john"
  38. -> createSession(groupID, authorID, validUntil)
  39. -> getSessionInfo(sessionID)
  40. -> listSessionsOfGroup(groupID) -- should be 1
  41. -> deleteSession(sessionID)
  42. -> getSessionInfo(sessionID) -- should have author id etc in
  43. -> listPads(groupID) -- should be empty array
  44. -> createGroupPad(groupID, padName [, text])
  45. -> listPads(groupID) -- should be empty array
  46. -> getPublicStatus(padId)
  47. -> setPublicStatus(padId, status)
  48. -> getPublicStatus(padId)
  49. -> isPasswordProtected(padID) -- should be false
  50. -> setPassword(padID, password)
  51. -> isPasswordProtected(padID) -- should be true
  52. -> listPadsOfAuthor(authorID)
  53. */
  54. describe('createGroup', function(){
  55. it('creates a new group', function(done) {
  56. api.get(endPoint('createGroup'))
  57. .expect(function(res){
  58. if(res.body.code !== 0 || !res.body.data.groupID) throw new Error("Unable to create new Pad");
  59. groupID = res.body.data.groupID;
  60. })
  61. .expect('Content-Type', /json/)
  62. .expect(200, done)
  63. });
  64. })
  65. describe('listSessionsOfGroup', function(){
  66. it('Lists the session of a group', function(done) {
  67. api.get(endPoint('listSessionsOfGroup')+"&groupID="+groupID)
  68. .expect(function(res){
  69. if(res.body.code !== 0 || res.body.data !== null) throw new Error("Sessions show as existing for this group");
  70. })
  71. .expect('Content-Type', /json/)
  72. .expect(200, done)
  73. });
  74. })
  75. describe('deleteGroup', function(){
  76. it('Deletes a group', function(done) {
  77. api.get(endPoint('deleteGroup')+"&groupID="+groupID)
  78. .expect(function(res){
  79. if(res.body.code !== 0) throw new Error("Group failed to be deleted");
  80. })
  81. .expect('Content-Type', /json/)
  82. .expect(200, done)
  83. });
  84. })
  85. describe('createGroupIfNotExistsFor', function(){
  86. it('Creates a group if one doesnt exist for mapper 0', function(done) {
  87. api.get(endPoint('createGroupIfNotExistsFor')+"&groupMapper=management")
  88. .expect(function(res){
  89. if(res.body.code !== 0 || !res.body.data.groupID) throw new Error("Sessions show as existing for this group");
  90. })
  91. .expect('Content-Type', /json/)
  92. .expect(200, done)
  93. });
  94. })
  95. describe('createGroup', function(){
  96. it('creates a new group', function(done) {
  97. api.get(endPoint('createGroup'))
  98. .expect(function(res){
  99. if(res.body.code !== 0 || !res.body.data.groupID) throw new Error("Unable to create new Pad");
  100. groupID = res.body.data.groupID;
  101. })
  102. .expect('Content-Type', /json/)
  103. .expect(200, done)
  104. });
  105. })
  106. describe('createAuthor', function(){
  107. it('Creates an author with a name set', function(done) {
  108. api.get(endPoint('createAuthor'))
  109. .expect(function(res){
  110. if(res.body.code !== 0 || !res.body.data.authorID) throw new Error("Unable to create author");
  111. })
  112. .expect('Content-Type', /json/)
  113. .expect(200, done)
  114. });
  115. })
  116. describe('createAuthor', function(){
  117. it('Creates an author with a name set', function(done) {
  118. api.get(endPoint('createAuthor')+"&name=john")
  119. .expect(function(res){
  120. if(res.body.code !== 0 || !res.body.data.authorID) throw new Error("Unable to create user with name set");
  121. authorID = res.body.data.authorID; // we will be this author for the rest of the tests
  122. })
  123. .expect('Content-Type', /json/)
  124. .expect(200, done)
  125. });
  126. })
  127. describe('createAuthorIfNotExistsFor', function(){
  128. it('Creates an author if it doesnt exist already and provides mapping', function(done) {
  129. api.get(endPoint('createAuthorIfNotExistsFor')+"&authorMapper=chris")
  130. .expect(function(res){
  131. if(res.body.code !== 0 || !res.body.data.authorID) throw new Error("Unable to create author with mapper");
  132. })
  133. .expect('Content-Type', /json/)
  134. .expect(200, done)
  135. });
  136. })
  137. describe('getAuthorName', function(){
  138. it('Gets the author name', function(done) {
  139. api.get(endPoint('getAuthorName')+"&authorID="+authorID)
  140. .expect(function(res){
  141. if(res.body.code !== 0 || res.body.data !== "john") throw new Error("Unable to get Author Name from Author ID");
  142. })
  143. .expect('Content-Type', /json/)
  144. .expect(200, done)
  145. });
  146. })
  147. // BEGIN SESSION TESTS
  148. ///////////////////////////////////////
  149. ///////////////////////////////////////
  150. describe('createSession', function(){
  151. it('Creates a session for an Author', function(done) {
  152. api.get(endPoint('createSession')+"&authorID="+authorID+"&groupID="+groupID+"&validUntil=999999999999")
  153. .expect(function(res){
  154. if(res.body.code !== 0 || !res.body.data.sessionID) throw new Error("Unable to create Session");
  155. sessionID = res.body.data.sessionID;
  156. })
  157. .expect('Content-Type', /json/)
  158. .expect(200, done)
  159. });
  160. })
  161. describe('getSessionInfo', function(){
  162. it('Gets session inf', function(done) {
  163. api.get(endPoint('getSessionInfo')+"&sessionID="+sessionID)
  164. .expect(function(res){
  165. if(res.body.code !== 0 || !res.body.data.groupID || !res.body.data.authorID || !res.body.data.validUntil) throw new Error("Unable to get Session info");
  166. })
  167. .expect('Content-Type', /json/)
  168. .expect(200, done)
  169. });
  170. })
  171. describe('listSessionsOfGroup', function(){
  172. it('Gets sessions of a group', function(done) {
  173. api.get(endPoint('listSessionsOfGroup')+"&groupID="+groupID)
  174. .expect(function(res){
  175. if(res.body.code !== 0 || typeof res.body.data !== "object") throw new Error("Unable to get sessions of a group");
  176. })
  177. .expect('Content-Type', /json/)
  178. .expect(200, done)
  179. });
  180. })
  181. describe('deleteSession', function(){
  182. it('Deletes a session', function(done) {
  183. api.get(endPoint('deleteSession')+"&sessionID="+sessionID)
  184. .expect(function(res){
  185. if(res.body.code !== 0) throw new Error("Unable to delete a session");
  186. })
  187. .expect('Content-Type', /json/)
  188. .expect(200, done)
  189. });
  190. })
  191. describe('getSessionInfo', function(){
  192. it('Gets session info', function(done) {
  193. api.get(endPoint('getSessionInfo')+"&sessionID="+sessionID)
  194. .expect(function(res){
  195. if(res.body.code !== 1) throw new Error("Session was not properly deleted");
  196. })
  197. .expect('Content-Type', /json/)
  198. .expect(200, done)
  199. });
  200. })
  201. // GROUP PAD MANAGEMENT
  202. ///////////////////////////////////////
  203. ///////////////////////////////////////
  204. describe('listPads', function(){
  205. it('Lists Pads of a Group', function(done) {
  206. api.get(endPoint('listPads')+"&groupID="+groupID)
  207. .expect(function(res){
  208. if(res.body.code !== 0 || res.body.data.padIDs.length !== 0) throw new Error("Group already had pads for some reason"+res.body.data.padIDs);
  209. })
  210. .expect('Content-Type', /json/)
  211. .expect(200, done)
  212. });
  213. })
  214. describe('createGroupPad', function(){
  215. it('Creates a Group Pad', function(done) {
  216. api.get(endPoint('createGroupPad')+"&groupID="+groupID+"&padName="+padID)
  217. .expect(function(res){
  218. if(res.body.code !== 0) throw new Error("Unable to create group pad");
  219. padID = res.body.data.padID;
  220. })
  221. .expect('Content-Type', /json/)
  222. .expect(200, done)
  223. });
  224. })
  225. describe('listPads', function(){
  226. it('Lists Pads of a Group', function(done) {
  227. api.get(endPoint('listPads')+"&groupID="+groupID)
  228. .expect(function(res){
  229. if(res.body.code !== 0 || res.body.data.padIDs.length !== 1) throw new Error("Group isnt listing this pad");
  230. })
  231. .expect('Content-Type', /json/)
  232. .expect(200, done)
  233. });
  234. })
  235. // PAD SECURITY /-_-\
  236. ///////////////////////////////////////
  237. ///////////////////////////////////////
  238. describe('getPublicStatus', function(){
  239. it('Gets the public status of a pad', function(done) {
  240. api.get(endPoint('getPublicStatus')+"&padID="+padID)
  241. .expect(function(res){
  242. if(res.body.code !== 0 || res.body.data.publicstatus) throw new Error("Unable to get public status of this pad");
  243. })
  244. .expect('Content-Type', /json/)
  245. .expect(200, done)
  246. });
  247. })
  248. describe('setPublicStatus', function(){
  249. it('Sets the public status of a pad', function(done) {
  250. api.get(endPoint('setPublicStatus')+"&padID="+padID+"&publicStatus=true")
  251. .expect(function(res){
  252. if(res.body.code !== 0) throw new Error("Setting status did not work");
  253. })
  254. .expect('Content-Type', /json/)
  255. .expect(200, done)
  256. });
  257. })
  258. describe('getPublicStatus', function(){
  259. it('Gets the public status of a pad', function(done) {
  260. api.get(endPoint('getPublicStatus')+"&padID="+padID)
  261. .expect(function(res){
  262. if(res.body.code !== 0 || !res.body.data.publicStatus) throw new Error("Setting public status of this pad did not work");
  263. })
  264. .expect('Content-Type', /json/)
  265. .expect(200, done)
  266. });
  267. })
  268. describe('isPasswordProtected', function(){
  269. it('Gets the public status of a pad', function(done) {
  270. api.get(endPoint('isPasswordProtected')+"&padID="+padID)
  271. .expect(function(res){
  272. if(res.body.code !== 0 || res.body.data.isPasswordProtected) throw new Error("Pad is password protected by default");
  273. })
  274. .expect('Content-Type', /json/)
  275. .expect(200, done)
  276. });
  277. })
  278. describe('setPassword', function(){
  279. it('Gets the public status of a pad', function(done) {
  280. api.get(endPoint('setPassword')+"&padID="+padID+"&password=test")
  281. .expect(function(res){
  282. if(res.body.code !== 0) throw new Error("Unabe to set password");
  283. })
  284. .expect('Content-Type', /json/)
  285. .expect(200, done)
  286. });
  287. })
  288. describe('isPasswordProtected', function(){
  289. it('Gets the public status of a pad', function(done) {
  290. api.get(endPoint('isPasswordProtected')+"&padID="+padID)
  291. .expect(function(res){
  292. if(res.body.code !== 0 || !res.body.data.isPasswordProtected) throw new Error("Pad password protection has not applied");
  293. })
  294. .expect('Content-Type', /json/)
  295. .expect(200, done)
  296. });
  297. })
  298. // NOT SURE HOW TO POPULAT THIS /-_-\
  299. ///////////////////////////////////////
  300. ///////////////////////////////////////
  301. describe('listPadsOfAuthor', function(){
  302. it('Gets the Pads of an Author', function(done) {
  303. api.get(endPoint('listPadsOfAuthor')+"&authorID="+authorID)
  304. .expect(function(res){
  305. if(res.body.code !== 0 || res.body.data.padIDs.length !== 0) throw new Error("Pad password protection has not applied");
  306. })
  307. .expect('Content-Type', /json/)
  308. .expect(200, done)
  309. });
  310. })
  311. var endPoint = function(point){
  312. return '/api/'+apiVersion+'/'+point+'?apikey='+apiKey;
  313. }
  314. function makeid()
  315. {
  316. var text = "";
  317. var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  318. for( var i=0; i < 5; i++ ){
  319. text += possible.charAt(Math.floor(Math.random() * possible.length));
  320. }
  321. return text;
  322. }