summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/config.yaml19
-rw-r--r--test/config/config.yaml5
-rw-r--r--test/integration/tapicero_test.rb37
-rw-r--r--test/integration/test_setup_test.rb15
-rw-r--r--test/setup_couch.sh13
-rw-r--r--test/support/integration_test.rb39
-rw-r--r--test/test_helper.rb6
-rw-r--r--test/unit/couch_changes_test.rb32
-rw-r--r--test/unit/couch_stream_test.rb29
9 files changed, 128 insertions, 67 deletions
diff --git a/test/config.yaml b/test/config.yaml
new file mode 100644
index 0000000..e59b4de
--- /dev/null
+++ b/test/config.yaml
@@ -0,0 +1,19 @@
+#
+# Default configuration options for Tapicero
+#
+
+# couch connection configuration
+connection:
+ protocol: "http"
+ host: "localhost"
+ port: 5984
+ username: anna
+ password: secret
+ prefix: "tapicero_test"
+ suffix: ""
+
+# file to store the last processed user record in so we can resume after
+# a restart:
+seq_file: "/tmp/tapicero_test.seq"
+log_file: "/tmp/tapicero_test.log"
+log_level: debug
diff --git a/test/config/config.yaml b/test/config/config.yaml
deleted file mode 100644
index ee10fe6..0000000
--- a/test/config/config.yaml
+++ /dev/null
@@ -1,5 +0,0 @@
-#
-# testing configuration options
-#
-
-db_prefix: "tapicero_test-"
diff --git a/test/integration/tapicero_test.rb b/test/integration/tapicero_test.rb
new file mode 100644
index 0000000..88e3715
--- /dev/null
+++ b/test/integration/tapicero_test.rb
@@ -0,0 +1,37 @@
+require_relative '../test_helper.rb'
+
+class TapiceroTest < Tapicero::IntegrationTest
+
+ def setup
+ create_user
+ end
+
+ def teardown
+ delete_user(true)
+ end
+
+ def test_creates_user_db
+ assert user_database
+ assert user_database.name.start_with?(config.options[:db_prefix])
+ assert user_database.info # ensure db exists in couch.
+ end
+
+ def test_configures_security
+ assert_equal config.options[:security], user_database.get('_security')
+ end
+
+ def test_stores_design_docs
+ assert_equal ['_design/docs', '_design/syncs', '_design/transactions'],
+ design_docs(user_database).map{|doc| doc["id"]}.sort
+ end
+
+ def test_cleares_user_db
+ assert user_database.info # ensure db exists in couch.
+ delete_user
+ assert !host.databases.include?(user_database.name)
+ end
+
+ def design_docs(db)
+ db.documents(startkey: '_design', endkey: '_design'.succ)["rows"]
+ end
+end
diff --git a/test/integration/test_setup_test.rb b/test/integration/test_setup_test.rb
new file mode 100644
index 0000000..525c14d
--- /dev/null
+++ b/test/integration/test_setup_test.rb
@@ -0,0 +1,15 @@
+require_relative '../test_helper.rb'
+
+class TestSetupTest < Tapicero::IntegrationTest
+
+ def test_loads_config
+ assert_equal "tapicero_test", config.connection[:prefix]
+ assert_equal "debug", config.send(:log_level)
+ end
+
+ def test_database_exists
+ assert database
+ assert_equal "tapicero_test_users", database.name
+ end
+
+end
diff --git a/test/setup_couch.sh b/test/setup_couch.sh
new file mode 100644
index 0000000..c11993d
--- /dev/null
+++ b/test/setup_couch.sh
@@ -0,0 +1,13 @@
+#!/bin/bash
+
+HOST="http://localhost:5984"
+echo "couch version :"
+curl -X GET $HOST
+echo "creating unprivileged user :"
+curl -HContent-Type:application/json -XPUT $HOST/_users/org.couchdb.user:me --data-binary '{"_id": "org.couchdb.user:me","name": "me","roles": [],"type": "user","password": "pwd"}'
+echo "creating database to watch:"
+curl -X PUT $HOST/tapicero_test_users
+echo "restricting database access :"
+curl -X PUT $HOST/tapicero_test_users/_security -Hcontent-type:application/json --data-binary '{"admins":{"names":[],"roles":[]},"members":{"names":["me"],"roles":[]}}'
+echo "adding admin :"
+curl -X PUT $HOST/_config/admins/anna -d '"secret"'
diff --git a/test/support/integration_test.rb b/test/support/integration_test.rb
new file mode 100644
index 0000000..b28c0e1
--- /dev/null
+++ b/test/support/integration_test.rb
@@ -0,0 +1,39 @@
+module Tapicero
+ class IntegrationTest < MiniTest::Unit::TestCase
+
+ def create_user(fast = false)
+ result = database.save_doc :some => :content
+ raise RuntimeError.new(result.inspect) unless result['ok']
+ sleep 1 unless fast # allow tapicero to do its job
+ @user = {'_id' => result["id"], '_rev' => result["rev"]}
+ end
+
+ def delete_user(fast = false)
+ return if @user.nil? or @user['_deleted']
+ result = database.delete_doc @user
+ raise RuntimeError.new(result.inspect) unless result['ok']
+ @user['_deleted'] = true
+ sleep 1 unless fast # allow tapicero to do its job
+ end
+
+ def user_database
+ host.database(config.options[:db_prefix] + @user['_id'])
+ end
+
+ def database
+ @database ||= host.database(database_name)
+ end
+
+ def database_name
+ config.complete_db_name('users')
+ end
+
+ def host
+ @host ||= CouchRest.new(config.couch_host)
+ end
+
+ def config
+ Tapicero.config
+ end
+ end
+end
diff --git a/test/test_helper.rb b/test/test_helper.rb
index 3857e2c..da8b88c 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -6,5 +6,9 @@ $:.unshift File.expand_path('lib', BASE_DIR)
require 'mocha/setup'
-TAPICERO_CONFIG = "test/config/config.yaml"
+require 'tapicero/version'
+Tapicero::CONFIGS << "test/config.yaml"
require 'tapicero'
+
+
+require_relative 'support/integration_test'
diff --git a/test/unit/couch_changes_test.rb b/test/unit/couch_changes_test.rb
deleted file mode 100644
index 043caf1..0000000
--- a/test/unit/couch_changes_test.rb
+++ /dev/null
@@ -1,32 +0,0 @@
-require File.expand_path('../../test_helper.rb', __FILE__)
-require 'tapicero/couch_changes'
-
-class CouchChangesTest < MiniTest::Unit::TestCase
-
- LAST_SEQ = 12
-
- def setup
- @stream = mock()
- @changes = Tapicero::CouchChanges.new(@stream)
- end
-
- def test_last_seq
- @stream.expects(:get).
- with('_changes', {:limit => 1, :descending => true}).
- yields(:last_seq => LAST_SEQ)
- assert_equal LAST_SEQ, @changes.last_seq
- end
-
- def test_follow
- stub_entry = {:new => :result}
- @stream.expects(:get).
- with('_changes', {:limit => 1, :descending => true}).
- yields(:last_seq => LAST_SEQ)
- @stream.expects(:get).
- with('_changes', {:feed => :continuous, :since => LAST_SEQ}).
- yields(stub_entry)
- @changes.follow do |hash|
- assert_equal stub_entry, hash
- end
- end
-end
diff --git a/test/unit/couch_stream_test.rb b/test/unit/couch_stream_test.rb
deleted file mode 100644
index a9de21f..0000000
--- a/test/unit/couch_stream_test.rb
+++ /dev/null
@@ -1,29 +0,0 @@
-require File.expand_path('../../test_helper.rb', __FILE__)
-require 'tapicero/couch_stream'
-
-class CouchStreamTest < MiniTest::Unit::TestCase
-
- def setup
- @root = "http://server/database"
- @stream = Tapicero::CouchStream.new(@root)
- @url = @root + "/_changes?a=b&c=d"
- @path = "_changes"
- @options = {:a => :b, :c => :d}
- end
-
- def test_get
- Tapicero::JsonStream.expects(:get).
- with(@url, :symbolize_keys => true).
- yields(stub_hash = stub)
- @stream.get(@path, @options) do |hash|
- assert_equal stub_hash, hash
- end
- end
-
- # internal
- def test_url_creation
- assert_equal "http://server/database/", @stream.send(:url_for, "")
- assert_equal @url, @stream.send(:url_for, @path, @options)
- end
-
-end