You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
5.3 KiB

  1. @testable import App
  2. import Fluent
  3. import XCTVapor
  4. final class UserTests: XCTestCase {
  5. func test1() async throws{
  6. let app = Application(.testing)
  7. defer{app.shutdown()}
  8. try await configure(app)
  9. let basepath = "/api/v2.0/users/"
  10. let fn = "Mike"
  11. let ln = "Carman"
  12. let email = "mcarman@rpmindustries.com"
  13. let username = "bryckman"
  14. let password = "password"
  15. let isactive = true
  16. let cuserid = -1
  17. let uuserid = -1
  18. //new
  19. try app.test(.POST, basepath, beforeRequest: {
  20. req in
  21. let input = mdlUser(id: nil, number: nil, firstname: fn, lastname: ln, username: username, password: password, email: email, isactive: isactive, createuserid: cuserid, updateuserid: uuserid, divisionid: nil, locationid: nil, gateid: nil)
  22. try req.content.encode(input)
  23. }, afterResponse: {
  24. res in
  25. XCTAssertEqual(res.status, .created)
  26. let obj = try res.content.decode(mdlUser.self)
  27. XCTAssertEqual(obj.firstname, fn)
  28. let id = obj.id
  29. //Update object just created
  30. try app.test(.POST, basepath, beforeRequest:{
  31. req in
  32. let input = mdlUser(id: id, number: "1", firstname: fn, lastname: ln, username: username, password: password, email: email, isactive: isactive, createuserid: cuserid, updateuserid: uuserid, divisionid: nil, locationid: nil, gateid: nil)
  33. try req.content.encode(input)
  34. }, afterResponse: {
  35. res in
  36. XCTAssertEqual(res.status, .accepted)
  37. let obj = try res.content.decode(mdlUser.self)
  38. XCTAssertEqual(obj.firstname, fn)
  39. })
  40. //All
  41. try app.test(.GET, basepath, afterResponse: { res in
  42. XCTAssertEqual(res.status, .ok)
  43. XCTAssertEqual(res.headers.contentType, .json)
  44. _ = try res.content.decode([mdlUser].self)
  45. })
  46. //read
  47. var parameter = try CustomCrypto.EncryptString(input: String(id!))
  48. var path = "\(basepath)\(parameter)"
  49. try app.test(.GET, path, afterResponse: { res in
  50. XCTAssertEqual(res.status, .ok)
  51. XCTAssertEqual(res.headers.contentType, .json)
  52. _ = try res.content.decode(mdlUser.self)
  53. })
  54. //login
  55. let gooduser = "bryckman"
  56. let goodpwd = "password"
  57. let baduser = "brycman_"
  58. let badpwd = "p@$$w0rd"
  59. var combo = "\(gooduser):\(goodpwd)"
  60. parameter = try CustomCrypto.EncryptString(input: combo)
  61. path = "\(basepath)login/\(parameter)"
  62. try app.test(.GET, path, afterResponse: {
  63. res in
  64. XCTAssertEqual(res.status, .ok)
  65. XCTAssertEqual(res.headers.contentType, .json)
  66. _ = try res.content.decode(mdlUser.self)
  67. })
  68. combo = "\(gooduser):\(badpwd)"
  69. parameter = try CustomCrypto.EncryptString(input: combo)
  70. path = "\(basepath)login/\(parameter)"
  71. try app.test(.GET, path, afterResponse: {
  72. res in
  73. XCTAssertEqual(res.status, .unauthorized)
  74. XCTAssertEqual(res.headers.contentType, .json)
  75. })
  76. combo = "\(baduser):\(badpwd)"
  77. parameter = try CustomCrypto.EncryptString(input: combo)
  78. path = "\(basepath)login/\(parameter)"
  79. try app.test(.GET, path, afterResponse: {
  80. res in
  81. XCTAssertEqual(res.status, .unauthorized)
  82. XCTAssertEqual(res.headers.contentType, .json)
  83. })
  84. combo = "\(baduser):\(goodpwd)"
  85. parameter = try CustomCrypto.EncryptString(input: combo)
  86. path = "\(basepath)login/\(parameter)"
  87. try app.test(.GET, path, afterResponse: {
  88. res in
  89. XCTAssertEqual(res.status, .unauthorized)
  90. XCTAssertEqual(res.headers.contentType, .json)
  91. })
  92. //null values
  93. combo = ":\(goodpwd)"
  94. parameter = try CustomCrypto.EncryptString(input: combo)
  95. path = "\(basepath)login/\(parameter)"
  96. try app.test(.GET, path, afterResponse: {
  97. res in
  98. XCTAssertEqual(res.status, .unauthorized)
  99. XCTAssertEqual(res.headers.contentType, .json)
  100. })
  101. combo = "\(baduser):"
  102. parameter = try CustomCrypto.EncryptString(input: combo)
  103. path = "\(basepath)login/\(parameter)"
  104. try app.test(.GET, path, afterResponse: {
  105. res in
  106. XCTAssertEqual(res.status, .unauthorized)
  107. XCTAssertEqual(res.headers.contentType, .json)
  108. })
  109. //delete user
  110. let encryptedid = try CustomCrypto.EncryptString(input: String(id!))
  111. path = "api/v2.0/users/"
  112. let url = "\(path)\(encryptedid)"
  113. try app.test(.DELETE, url, afterResponse: {
  114. res in
  115. XCTAssertEqual(res.status, .ok)
  116. _ = try res.content.decode(mdlUser.self)
  117. })
  118. })
  119. }
  120. }