From 51cdc6d2d20ca1da249e516a797d15237a9136c9 Mon Sep 17 00:00:00 2001 From: Ludovic 'Archivist' Lagouardette Date: Sun, 2 Sep 2018 18:58:28 +0200 Subject: [PATCH] Unit tests enhancements --- spec/sales_backend_spec.cr | 86 +++++++++++++++++++++++----------- src/sales_backend/http/user.cr | 3 ++ 2 files changed, 61 insertions(+), 28 deletions(-) diff --git a/spec/sales_backend_spec.cr b/spec/sales_backend_spec.cr index a6280a9..ccebaf3 100644 --- a/spec/sales_backend_spec.cr +++ b/spec/sales_backend_spec.cr @@ -1,37 +1,67 @@ require "./spec_helper" - describe SalesBackend do it "can render status" do - get "/" - String.from_json(Global.response.not_nil!.body).should eq "OK" + get "/" + String.from_json(Global.response.not_nil!.body).should eq "OK" end it "can add a user able to log in and out" do - Dir.mkdir_p Statics.data_path+"user" - usr = User.new("dummy@domain.com") - usr.password_hash = "mywordismypassword" - - post "/user", nil, usr.to_json - Global.response.not_nil!.status_code.should eq(200) - String.from_json(Global.response.not_nil!.body).should eq "OK" - - post "/login", nil, usr.to_json - Global.response.not_nil!.status_code.should eq(200) - uuid=UUID.from_json(Global.response.not_nil!.body).not_nil! - - headers = HTTP::Headers.new - headers["user"]=usr.email.to_s - headers["api_token"]=uuid.to_s - get "/user/", headers - Global.response.not_nil!.status_code.should eq(200) - - usr.tokens = Array(UUID).new - usr.tokens.not_nil!.push uuid - post "/logout", nil, usr.to_json - Global.response.not_nil!.status_code.should eq(200) - - get "/user/", headers - Global.response.not_nil!.status_code.should eq(403) + Dir.mkdir_p Statics.data_path + "user" + usr = User.new("dummy@domain.com") + usr.password_hash = "mywordismypassword" + + post "/user", nil, usr.to_json + Global.response.not_nil!.status_code.should eq(200) + String.from_json(Global.response.not_nil!.body).should eq "OK" + + post "/user", nil, usr.to_json + Global.response.not_nil!.status_code.should eq(500) + + post "/login", nil, usr.to_json + Global.response.not_nil!.status_code.should eq(200) + uuid = UUID.from_json(Global.response.not_nil!.body).not_nil! + + headers = HTTP::Headers.new + headers["user"] = usr.email.to_s + headers["api_token"] = uuid.to_s + get "/user/", headers + Global.response.not_nil!.status_code.should eq(200) + get "/user/address", headers + Global.response.not_nil!.status_code.should eq(200) + Global.response.not_nil!.body.should eq("null") + new_address_str = %([ + { + "name": "String", + "address1": "String", + "address2": "{type: String, nilable: true}", + "postcode": "00000", + "city": "String", + "country": 2 + } + ]) + post "/user/address", headers, new_address_str + Global.response.not_nil!.status_code.should eq(200) + get "/user/address", headers + Global.response.not_nil!.status_code.should eq(200) + address_state = Array(Address).from_json(Global.response.not_nil!.body) + expected_address_state = Array(Address).from_json(new_address_str) + (address_state.to_json==expected_address_state.to_json).should be_true + get "/user/tokens", headers + Global.response.not_nil!.status_code.should eq(200) + + usr.tokens = Array(UUID).new + usr.tokens.not_nil!.push uuid + post "/logout", nil, usr.to_json + Global.response.not_nil!.status_code.should eq(200) + + get "/user/", headers + Global.response.not_nil!.status_code.should eq(403) + + get "/user/address", headers + Global.response.not_nil!.status_code.should eq(403) + + get "/user/tokens", headers + Global.response.not_nil!.status_code.should eq(403) end end diff --git a/src/sales_backend/http/user.cr b/src/sales_backend/http/user.cr index 9899e9d..e049f46 100644 --- a/src/sales_backend/http/user.cr +++ b/src/sales_backend/http/user.cr @@ -95,6 +95,9 @@ post "/user" do |context| if Statics.email_regex.match(user.email)==nil raise Exception.new("Bad email address") end + if File.exists?(Statics.data_path+"user/"+user.email) + raise Exception.new("Email address already in use") + end File.write(Statics.data_path+"user/"+user.email,user.to_json) context.response.content_type = "application/json" "OK".to_json