mirror of
https://github.com/hkalexling/Mango.git
synced 2025-08-03 11:25:29 -04:00
Add unit test
This commit is contained in:
parent
46b36860d1
commit
bc75f4d336
3
Makefile
3
Makefile
@ -19,6 +19,9 @@ libs:
|
|||||||
run:
|
run:
|
||||||
crystal run src/mango.cr --error-trace
|
crystal run src/mango.cr --error-trace
|
||||||
|
|
||||||
|
test:
|
||||||
|
crystal spec
|
||||||
|
|
||||||
install:
|
install:
|
||||||
cp mango $(INSTALL_DIR)/mango
|
cp mango $(INSTALL_DIR)/mango
|
||||||
|
|
||||||
|
2
spec/asset/test-config.yml
Normal file
2
spec/asset/test-config.yml
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
---
|
||||||
|
port: 3000
|
14
spec/config_spec.cr
Normal file
14
spec/config_spec.cr
Normal 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
3
spec/spec_helper.cr
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
require "spec"
|
||||||
|
require "../src/context"
|
||||||
|
require "../src/server"
|
66
spec/storage_spec.cr
Normal file
66
spec/storage_spec.cr
Normal 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
|
Loading…
x
Reference in New Issue
Block a user