mirror of https://github.com/getmango/Mango.git
Better state management in spec and add some tests for MangaDex::Queue
This commit is contained in:
parent
9ffe896705
commit
1abdac2fdd
|
@ -2,11 +2,11 @@ require "./spec_helper"
|
||||||
|
|
||||||
describe Config do
|
describe Config do
|
||||||
it "creates config if it does not exist" do
|
it "creates config if it does not exist" do
|
||||||
tempfile = File.tempfile "mango-test-config"
|
with_default_config do |config, logger, path|
|
||||||
config = Config.load tempfile.path
|
File.exists?(path).should be_true
|
||||||
File.exists?(tempfile.path).should be_true
|
end
|
||||||
tempfile.delete
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "correctly loads config" do
|
it "correctly loads config" do
|
||||||
config = Config.load "spec/asset/test-config.yml"
|
config = Config.load "spec/asset/test-config.yml"
|
||||||
config.port.should eq 3000
|
config.port.should eq 3000
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -1,3 +1,65 @@
|
||||||
require "spec"
|
require "spec"
|
||||||
require "../src/context"
|
require "../src/context"
|
||||||
require "../src/server"
|
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
|
||||||
|
|
|
@ -1,66 +1,91 @@
|
||||||
require "./spec_helper"
|
require "./spec_helper"
|
||||||
|
|
||||||
describe Storage do
|
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
|
it "creates DB at given path" do
|
||||||
storage = Storage.new temp_db.path, MLogger.new config
|
with_storage do |storage, path|
|
||||||
File.exists?(temp_db.path).should be_true
|
File.exists?(path).should be_true
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "deletes user" do
|
it "deletes user" do
|
||||||
storage = Storage.new temp_db.path, MLogger.new config
|
with_storage do |storage|
|
||||||
storage.delete_user "admin"
|
storage.delete_user "admin"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "creates new user" do
|
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 "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
|
storage.new_user "admin", "123456", true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
it "verifies token" do
|
|
||||||
storage = Storage.new temp_db.path, MLogger.new config
|
it "verifies username/password combination" do
|
||||||
token = storage.verify_token user_token
|
with_storage do |storage|
|
||||||
token.should eq "user"
|
user_token = storage.verify_user "user", "123456"
|
||||||
end
|
admin_token = storage.verify_user "admin", "123456"
|
||||||
it "verfies admin token" do
|
user_token.should_not be_nil
|
||||||
storage = Storage.new temp_db.path, MLogger.new config
|
admin_token.should_not be_nil
|
||||||
storage.verify_admin(admin_token).should be_true
|
State.set "user_token", user_token
|
||||||
end
|
State.set "admin_token", admin_token
|
||||||
it "rejects non-admin token" do
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
temp_config.delete
|
it "rejects duplicate username" do
|
||||||
temp_db.delete
|
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
|
end
|
||||||
|
|
Loading…
Reference in New Issue