diff --git a/spec/config_spec.cr b/spec/config_spec.cr index 3299a0e..c4f8700 100644 --- a/spec/config_spec.cr +++ b/spec/config_spec.cr @@ -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 diff --git a/spec/mangadex_spec.cr b/spec/mangadex_spec.cr new file mode 100644 index 0000000..6055ab7 --- /dev/null +++ b/spec/mangadex_spec.cr @@ -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 + diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index 3040af2..fa4409b 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -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 diff --git a/spec/storage_spec.cr b/spec/storage_spec.cr index 2b42711..db1400e 100644 --- a/spec/storage_spec.cr +++ b/spec/storage_spec.cr @@ -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 - storage.delete_user "admin" + with_storage do |storage| + storage.delete_user "admin" + end 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 + with_storage do |storage| + storage.new_user "user", "123456", false 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 + + 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 - temp_config.delete - temp_db.delete + 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 + + 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 + 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 + with_storage do |storage| + user_token = State.get! "user_token" + storage.verify_admin(user_token).should be_false + end + end + + 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 + + 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 + + it "cleans up" do + State.reset + with_storage do + true + end + end end