Better state management in spec and add some tests for MangaDex::Queue

This commit is contained in:
Alex Ling 2020-03-03 18:34:39 +00:00
parent 9ffe896705
commit 1abdac2fdd
4 changed files with 183 additions and 56 deletions

View File

@ -2,11 +2,11 @@ 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
with_default_config do |config, logger, path|
File.exists?(path).should be_true
end
end
it "correctly loads config" do
config = Config.load "spec/asset/test-config.yml"
config.port.should eq 3000

40
spec/mangadex_spec.cr Normal file
View File

@ -0,0 +1,40 @@
require "./spec_helper"
include MangaDex
describe Queue do
it "creates DB at given path" do
with_queue do |queue, path|
File.exists?(path).should be_true
end
end
it "inserts multiple jobs" do
with_queue do |queue|
j1 = Job.new "1", "1", "title", "manga_title", JobStatus::Error,
Time.utc
j2 = Job.new "2", "2", "title", "manga_title", JobStatus::Completed,
Time.utc
j3 = Job.new "0", "0", "title", "manga_title", JobStatus::Pending,
Time.utc
count = queue.push [j1, j2, j3]
count.should eq 3
end
end
it "pops pending job" do
with_queue do |queue|
job = queue.pop
job.should_not be_nil
job.not_nil!.id.should eq "0"
end
end
it "cleans up" do
State.reset
with_queue do
true
end
end
end

View File

@ -1,3 +1,65 @@
require "spec"
require "../src/context"
require "../src/server"
class State
@@hash = {} of String => String
def self.get(key)
@@hash[key]?
end
def self.get!(key)
@@hash[key]
end
def self.set(key, value)
return if value.nil?
@@hash[key] = value
end
def self.reset
@@hash.clear
end
end
def get_tempfile(name)
path = State.get name
if path.nil? || !File.exists? path
file = File.tempfile name
State.set name, file.path
return file
else
return File.new path
end
end
def with_default_config
temp_config = get_tempfile "mango-test-config"
config = Config.load temp_config.path
logger = MLogger.new config
yield config, logger, temp_config.path
temp_config.delete
end
def with_storage
with_default_config do |config, logger|
temp_db = get_tempfile "mango-test-db"
storage = Storage.new temp_db.path, logger
clear = yield storage, temp_db.path
if clear == true
temp_db.delete
end
end
end
def with_queue
with_default_config do |config, logger|
temp_queue_db = get_tempfile "mango-test-queue-db"
queue = MangaDex::Queue.new temp_queue_db.path, logger
clear = yield queue, temp_queue_db.path
if clear == true
temp_queue_db.delete
end
end
end

View File

@ -1,66 +1,91 @@
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
with_storage do |storage, path|
File.exists?(path).should be_true
end
end
it "deletes user" do
storage = Storage.new temp_db.path, MLogger.new config
with_storage do |storage|
storage.delete_user "admin"
end
end
it "creates new user" do
storage = Storage.new temp_db.path, MLogger.new config
with_storage do |storage|
storage.new_user "user", "123456", false
storage.new_user "admin", "123456", true
end
end
it "verifies username/password combination" do
storage = Storage.new temp_db.path, MLogger.new config
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
it "rejects duplicate username" do
storage = Storage.new temp_db.path, MLogger.new config
with_storage do |storage|
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 "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
it "verfies admin token" do
storage = Storage.new temp_db.path, MLogger.new config
with_storage do |storage|
admin_token = State.get! "admin_token"
storage.verify_admin(admin_token).should be_true
end
end
it "rejects non-admin token" do
storage = Storage.new temp_db.path, MLogger.new config
with_storage do |storage|
user_token = State.get! "user_token"
storage.verify_admin(user_token).should be_false
end
end
it "updates user" do
storage = Storage.new temp_db.path, MLogger.new config
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
it "logs user out" do
storage = Storage.new temp_db.path, MLogger.new config
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
temp_config.delete
temp_db.delete
end
it "cleans up" do
State.reset
with_storage do
true
end
end
end