Implementation of a generic backend of eshop in Crystal
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.

103 regels
3.1 KiB

6 jaren geleden
6 jaren geleden
  1. require "./spec_helper"
  2. describe SalesBackend do
  3. it "can render status" do
  4. get "/"
  5. String.from_json(Global.response.not_nil!.body).should eq "OK"
  6. end
  7. it "can compare addresses" do
  8. address_str1 = Address.from_json %(
  9. {
  10. "name": "String",
  11. "address1": "String",
  12. "address2": "{type: String, nilable: true}",
  13. "postcode": "00001",
  14. "city": "String",
  15. "country": 2
  16. }
  17. )
  18. address_str2 = Address.from_json %(
  19. {
  20. "name": "String",
  21. "address1": "String",
  22. "address2": "{type: String, nilable: true}",
  23. "postcode": "00002",
  24. "city": "String",
  25. "country": 2
  26. }
  27. )
  28. address_str3 = Address.from_json %(
  29. {
  30. "name": "String",
  31. "address1": "String",
  32. "address2": "{type: String, nilable: true}",
  33. "postcode": "00003",
  34. "city": "String",
  35. "country": 2
  36. }
  37. )
  38. (address_str1<=>address_str1).should eq( 0)
  39. (address_str3<=>address_str2).should eq(1)
  40. (address_str1<=>address_str2).should eq(-1)
  41. end
  42. it "can add a user able to log in and out" do
  43. Dir.mkdir_p Statics.data_path + "user"
  44. usr = User.new("dummy@domain.com")
  45. usr.password_hash = "mywordismypassword"
  46. post "/user", nil, usr.to_json
  47. Global.response.not_nil!.status_code.should eq(200)
  48. String.from_json(Global.response.not_nil!.body).should eq "OK"
  49. post "/user", nil, usr.to_json
  50. Global.response.not_nil!.status_code.should eq(500)
  51. post "/login", nil, usr.to_json
  52. Global.response.not_nil!.status_code.should eq(200)
  53. uuid = UUID.from_json(Global.response.not_nil!.body).not_nil!
  54. headers = HTTP::Headers.new
  55. headers["user"] = usr.email.to_s
  56. headers["api_token"] = uuid.to_s
  57. get "/user/", headers
  58. Global.response.not_nil!.status_code.should eq(200)
  59. get "/user/address", headers
  60. Global.response.not_nil!.status_code.should eq(200)
  61. Global.response.not_nil!.body.should eq("null")
  62. new_address_str = %([
  63. {
  64. "name": "String",
  65. "address1": "String",
  66. "address2": "{type: String, nilable: true}",
  67. "postcode": "00000",
  68. "city": "String",
  69. "country": 2
  70. }
  71. ])
  72. post "/user/address", headers, new_address_str
  73. Global.response.not_nil!.status_code.should eq(200)
  74. get "/user/address", headers
  75. Global.response.not_nil!.status_code.should eq(200)
  76. address_state = Array(Address).from_json(Global.response.not_nil!.body)
  77. expected_address_state = Array(Address).from_json(new_address_str)
  78. (address_state.to_json == expected_address_state.to_json).should be_true
  79. get "/user/tokens", headers
  80. Global.response.not_nil!.status_code.should eq(200)
  81. usr.tokens = Array(UUID).new
  82. usr.tokens.not_nil!.push uuid
  83. post "/logout", nil, usr.to_json
  84. Global.response.not_nil!.status_code.should eq(200)
  85. get "/user/", headers
  86. Global.response.not_nil!.status_code.should eq(403)
  87. get "/user/address", headers
  88. Global.response.not_nil!.status_code.should eq(403)
  89. get "/user/tokens", headers
  90. Global.response.not_nil!.status_code.should eq(403)
  91. end
  92. end