Add unit test

This commit is contained in:
Alex Ling 2020-02-25 19:00:10 +00:00
parent 46b36860d1
commit bc75f4d336
5 changed files with 88 additions and 0 deletions

View File

@ -19,6 +19,9 @@ libs:
run:
crystal run src/mango.cr --error-trace
test:
crystal spec
install:
cp mango $(INSTALL_DIR)/mango

View File

@ -0,0 +1,2 @@
---
port: 3000

14
spec/config_spec.cr Normal file
View File

@ -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
spec/spec_helper.cr Normal file
View File

@ -0,0 +1,3 @@
require "spec"
require "../src/context"
require "../src/server"

66
spec/storage_spec.cr Normal file
View File

@ -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