Guest User

Untitled

a guest
Jan 24th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. /**
  2. * INSTRUCTIONS
  3. *
  4. * install: npm install expresso
  5. *
  6. * (on project folder) run: express -s
  7. *
  8. */
  9.  
  10.  
  11.  
  12.  
  13. var app = require('../app.js'),
  14. async = require('async'),
  15. qs = require('qs'),
  16. assert = require('assert'),
  17. cookie = "",
  18. asserted_login = qs.stringify( {
  19. user: {
  20. username: 'lab',
  21. password: '123456',
  22. }
  23. }),
  24. // All tests that do not require Auth to run
  25. unauth_tests = {},
  26. // All tests that require cookie to be signed
  27. authed_tests = {}
  28.  
  29.  
  30.  
  31. /*
  32. * Index should respond as HTML5
  33. */
  34. unauth_tests.rootUrl = function(){
  35. console.log("RUN",1)
  36. assert.response( app, {
  37. url: "/",
  38. timeout: 200,
  39. method: 'GET',
  40. }, {
  41. status: 200,
  42. headers: {
  43. 'Content-Type': 'text/html; charset=utf-8'
  44. }
  45. },
  46.  
  47. function(res){
  48. //console.info(res.headers)
  49. //console.info('0')
  50. assert.match(res.body, /^\<\!DOCTYPE html/, "is HTML5?")
  51. },
  52. "Asserting index page"
  53. )
  54. }
  55.  
  56. /*
  57. * Before loggin in client should _NOT_ be allowed to access the data
  58. */
  59. unauth_tests.userIndexAccessDenied = function(){
  60. console.log("RUN",2)
  61. assert.response( app, {
  62. url: "/admin/users",
  63. timeout: 200,
  64. method: 'GET',
  65. }, {
  66. status: 200,
  67. },
  68.  
  69. function(res){
  70. //console.info(res.body)
  71. var res_data = JSON.parse(res.body)
  72. assert.length(res_data, 1)
  73. assert.type(res_data[0], 'string')
  74. },
  75. "Restricted area off-limits!"
  76. )
  77. }
  78.  
  79.  
  80. /*
  81. * Empty parameters, should make validation fail
  82. */
  83. unauth_tests.postEmptyLoginFail = function(){
  84. console.log("RUN",3)
  85. assert.response( app, {
  86. url: '/login',
  87. //timeout: 300,
  88. method: 'POST',
  89. data: ""
  90. },
  91. {
  92. status: 200,
  93. body: '["FAIL"]',
  94. headers: {
  95. 'Content-Type': 'application/json; charset=utf-8'
  96. },
  97. }
  98. )
  99. }
  100.  
  101. /*
  102. * Wrong parameters, should make validation fail, alike empty params
  103. */
  104. unauth_tests.postWrongLoginFail = function(){
  105. console.log("RUN",4)
  106. assert.response( app, {
  107. url: '/login',
  108. //timeout: 300,
  109. method: 'POST',
  110. data: asserted_login+'--notnot',
  111. },
  112. {
  113. status: 200,
  114. body: '["FAIL"]',
  115. headers: {
  116. 'Content-Type': 'application/json; charset=utf-8'
  117. },
  118. }
  119. )
  120. }
  121.  
  122.  
  123. /*
  124. * Valid client and params, login, return a access cookie!
  125. */
  126. unauth_tests.postLoginWin = function(){
  127. console.log("RUN",5)
  128. assert.response( app, {
  129. url: '/login',
  130. //timeout: 300,
  131. data: asserted_login,
  132. method: 'POST',
  133. },
  134. {
  135. status: 200,
  136. body: '["OK"]',
  137. headers: {
  138. 'Content-Type': 'application/json; charset=utf-8'
  139. },
  140. },
  141. function(res){
  142. //console.info('-3')
  143. cookie = res.headers['set-cookie'][0].split(';')[0]
  144.  
  145. append_authed_tests()
  146. },
  147.  
  148. "Asseting the login key-value, right"
  149. )
  150. }
  151.  
  152.  
  153.  
  154. for( var prop in unauth_tests){
  155. exports[prop] = unauth_tests[prop]
  156. }
  157.  
  158.  
  159.  
  160. setTimeout(function(){
  161. exports['test async exports'] = function(){
  162. console.log("RUN",6)
  163. console.log("!!! I, late appending.. will run?() RAN !!")
  164. assert.isNull(null);
  165. };
  166. }, 1000);
  167.  
  168.  
  169. /*
  170. * After .postLoginWin ran, we can run the authed tests, so only then, append then
  171. */
  172. function append_authed_tests(){
  173. for( var prop in authed_tests){
  174. console.log('appending', prop)
  175. exports[prop] = authed_tests[prop]
  176. }
  177. }
  178.  
  179.  
  180. /*
  181. * Authed test, requires signed cookie
  182. */
  183. authed_tests.getUsers = function(){
  184. console.log("RUN",7)
  185. assert.response( app, {
  186. url: '/admin/users',
  187. //timeout: 300,
  188. data: asserted_login,
  189. headers: {
  190. 'Cookie': cookie,
  191. },
  192. method: 'GET',
  193. },
  194. {
  195. status: 200,
  196. //body: '["OK"]',
  197. headers: {
  198. 'Content-Type': 'application/json; charset=utf-8'
  199. },
  200. },
  201. function(res){
  202. //console.info('-3')
  203.  
  204. },
  205.  
  206. "Get a list of all users in the DB"
  207. )
  208. }
Add Comment
Please, Sign In to add comment