Browse Source

Added proper unit testing

master
Ludovic 'Archivist' Lagouardette 5 years ago
parent
commit
d1d047a9f5
8 changed files with 55 additions and 5 deletions
  1. +5
    -0
      Dockerfile
  2. +4
    -0
      shard.lock
  3. +3
    -0
      shard.yml
  4. +31
    -3
      spec/sales_backend_spec.cr
  5. +1
    -1
      spec/spec_helper.cr
  6. +3
    -0
      src/config.cr
  7. +7
    -1
      src/sales_backend/http/user.cr
  8. +1
    -0
      src/sales_backend/user.cr

+ 5
- 0
Dockerfile View File

@ -4,6 +4,11 @@ RUN apk add --no-cache crystal shards openssl openssl-dev musl-dev libc6-compat
COPY . /opt/app/
RUN cd /opt/app && \
shards
ENV KEMAL_ENV test
RUN cd /opt/app/ && crystal spec
ENV KEMAL_ENV production
RUN cd /opt/app/ && crystal build --mcpu $(gcc -march=native -Q --help=target|grep march|awk '{print $2}'|head -n 1) --release src/sales_backend.cr

+ 4
- 0
shard.lock View File

@ -12,3 +12,7 @@ shards:
github: luislavena/radix
version: 0.3.8
spec-kemal:
github: kemalcr/spec-kemal
version: 0.4.0

+ 3
- 0
shard.yml View File

@ -13,5 +13,8 @@ crystal: 0.25.0
dependencies:
kemal:
github: kemalcr/kemal
version: 0.23.0
spec-kemal:
github: kemalcr/spec-kemal
license: MIT

+ 31
- 3
spec/sales_backend_spec.cr View File

@ -1,9 +1,37 @@
require "./spec_helper"
describe SalesBackend do
# TODO: Write tests
it "can render status" do
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)
it "works" do
false.should eq(true)
get "/user/", headers
Global.response.not_nil!.status_code.should eq(403)
end
end

+ 1
- 1
spec/spec_helper.cr View File

@ -1,2 +1,2 @@
require "spec"
require "spec-kemal"
require "../src/sales_backend"

+ 3
- 0
src/config.cr View File

@ -1,5 +1,8 @@
class Statics
def self.data_path
if ENV["KEMAL_ENV"] == "test"
return "/tmp/"
end
"/opt/app/data/"
end

+ 7
- 1
src/sales_backend/http/user.cr View File

@ -160,5 +160,11 @@ end
get "/user" do |context|
context.response.content_type = "application/json"
authenticate!(context.request.headers["user"],UUID.new(context.request.headers["api_token"])).to_json
user : User | Nil
begin
user = authenticate!(context.request.headers["user"],UUID.new(context.request.headers["api_token"]))
rescue ex
halt context, status_code: 403, response: ex.to_s
end
user.not_nil!
end

+ 1
- 0
src/sales_backend/user.cr View File

@ -19,4 +19,5 @@ class User
active: {type: Bool, default: false},
type: {type: UserType, default: UserType::Normal},
)
def initialize(@email, @active = false, @type = UserType::Normal) end
end

Loading…
Cancel
Save