2020-02-25 19:00:10 +00:00
|
|
|
require "./spec_helper"
|
|
|
|
|
|
|
|
describe Storage do
|
2020-04-08 03:38:02 +00:00
|
|
|
it "creates DB at given path" do
|
2020-04-08 06:41:27 +00:00
|
|
|
with_storage do |_, path|
|
2020-04-08 03:38:02 +00:00
|
|
|
File.exists?(path).should be_true
|
|
|
|
end
|
|
|
|
end
|
2020-03-03 18:34:39 +00:00
|
|
|
|
2020-04-08 03:38:02 +00:00
|
|
|
it "deletes user" do
|
2021-03-12 15:03:12 +00:00
|
|
|
with_storage &.delete_user "admin"
|
2020-04-08 03:38:02 +00:00
|
|
|
end
|
2020-03-03 18:34:39 +00:00
|
|
|
|
2020-04-08 03:38:02 +00:00
|
|
|
it "creates new user" do
|
|
|
|
with_storage do |storage|
|
|
|
|
storage.new_user "user", "123456", false
|
|
|
|
storage.new_user "admin", "123456", true
|
|
|
|
end
|
|
|
|
end
|
2020-03-03 18:34:39 +00:00
|
|
|
|
2020-04-08 03:38:02 +00:00
|
|
|
it "verifies username/password combination" do
|
|
|
|
with_storage do |storage|
|
|
|
|
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
|
|
|
|
State.set "user_token", user_token
|
|
|
|
State.set "admin_token", admin_token
|
|
|
|
end
|
|
|
|
end
|
2020-03-03 18:34:39 +00:00
|
|
|
|
2020-04-08 03:38:02 +00:00
|
|
|
it "rejects duplicate username" do
|
|
|
|
with_storage do |storage|
|
|
|
|
expect_raises SQLite3::Exception,
|
|
|
|
"UNIQUE constraint failed: users.username" do
|
|
|
|
storage.new_user "admin", "123456", true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-03-03 18:34:39 +00:00
|
|
|
|
2020-04-08 03:38:02 +00:00
|
|
|
it "verifies token" do
|
|
|
|
with_storage do |storage|
|
|
|
|
user_token = State.get! "user_token"
|
|
|
|
user = storage.verify_token user_token
|
|
|
|
user.should eq "user"
|
|
|
|
end
|
|
|
|
end
|
2020-03-03 18:34:39 +00:00
|
|
|
|
2020-04-08 03:38:02 +00:00
|
|
|
it "verfies admin token" do
|
|
|
|
with_storage do |storage|
|
|
|
|
admin_token = State.get! "admin_token"
|
|
|
|
storage.verify_admin(admin_token).should be_true
|
|
|
|
end
|
|
|
|
end
|
2020-03-03 18:34:39 +00:00
|
|
|
|
2020-04-08 03:38:02 +00:00
|
|
|
it "rejects non-admin token" do
|
|
|
|
with_storage do |storage|
|
|
|
|
user_token = State.get! "user_token"
|
|
|
|
storage.verify_admin(user_token).should be_false
|
|
|
|
end
|
|
|
|
end
|
2020-03-03 18:34:39 +00:00
|
|
|
|
2020-04-08 03:38:02 +00:00
|
|
|
it "updates user" do
|
|
|
|
with_storage do |storage|
|
|
|
|
storage.update_user "admin", "admin", "654321", true
|
|
|
|
token = storage.verify_user "admin", "654321"
|
|
|
|
admin_token = State.get! "admin_token"
|
|
|
|
token.should eq admin_token
|
|
|
|
end
|
|
|
|
end
|
2020-03-03 18:34:39 +00:00
|
|
|
|
2020-04-08 03:38:02 +00:00
|
|
|
it "logs user out" do
|
|
|
|
with_storage do |storage|
|
|
|
|
user_token = State.get! "user_token"
|
|
|
|
admin_token = State.get! "admin_token"
|
|
|
|
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
|
|
|
|
end
|
2020-02-25 19:00:10 +00:00
|
|
|
|
2020-04-08 03:38:02 +00:00
|
|
|
it "cleans up" do
|
|
|
|
with_storage do
|
|
|
|
true
|
|
|
|
end
|
|
|
|
State.reset
|
|
|
|
end
|
2020-02-25 19:00:10 +00:00
|
|
|
end
|