Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
hkalexling committed Feb 25, 2020
1 parent 46b3686 commit bc75f4d
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ libs:
run:
crystal run src/mango.cr --error-trace

test:
crystal spec

install:
cp mango $(INSTALL_DIR)/mango

Expand Down
2 changes: 2 additions & 0 deletions spec/asset/test-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
port: 3000
14 changes: 14 additions & 0 deletions spec/config_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require "./spec_helper"

describe Config do
it "creates config if it does not exist" do
tempfile = File.tempfile "mango-test-config"
config = Config.load tempfile.path
File.exists?(tempfile.path).should be_true
tempfile.delete
end
it "correctly loads config" do
config = Config.load "spec/asset/test-config.yml"
config.port.should eq 3000
end
end
3 changes: 3 additions & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require "spec"
require "../src/context"
require "../src/server"
66 changes: 66 additions & 0 deletions spec/storage_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
require "./spec_helper"

describe Storage do
temp_config = File.tempfile "mango-test-config"
temp_db = File.tempfile "mango-test-db"
config = Config.load temp_config.path
user_token = nil
admin_token = nil

it "creates DB at given path" do
storage = Storage.new temp_db.path, MLogger.new config
File.exists?(temp_db.path).should be_true
end
it "deletes user" do
storage = Storage.new temp_db.path, MLogger.new config
storage.delete_user "admin"
end
it "creates new user" do
storage = Storage.new temp_db.path, MLogger.new config
storage.new_user "user", "123456", false
storage.new_user "admin", "123456", true
end
it "verifies username/password combination" do
storage = Storage.new temp_db.path, MLogger.new config
user_token = storage.verify_user "user", "123456"
admin_token = storage.verify_user "admin", "123456"
user_token.should_not be_nil
admin_token.should_not be_nil
end
it "rejects duplicate username" do
storage = Storage.new temp_db.path, MLogger.new config
expect_raises SQLite3::Exception,
"UNIQUE constraint failed: users.username" do
storage.new_user "admin", "123456", true
end
end
it "verifies token" do
storage = Storage.new temp_db.path, MLogger.new config
token = storage.verify_token user_token
token.should eq "user"
end
it "verfies admin token" do
storage = Storage.new temp_db.path, MLogger.new config
storage.verify_admin(admin_token).should be_true
end
it "rejects non-admin token" do
storage = Storage.new temp_db.path, MLogger.new config
storage.verify_admin(user_token).should be_false
end
it "updates user" do
storage = Storage.new temp_db.path, MLogger.new config
storage.update_user "admin", "admin", "654321", true
token = storage.verify_user "admin", "654321"
token.should eq admin_token
end
it "logs user out" do
storage = Storage.new temp_db.path, MLogger.new config
storage.logout user_token
storage.logout admin_token
storage.verify_token(user_token).should be_nil
storage.verify_token(admin_token).should be_nil
end

temp_config.delete
temp_db.delete
end

0 comments on commit bc75f4d

Please sign in to comment.