vines-sql 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile +3 -0
- data/LICENSE +19 -0
- data/README.md +53 -0
- data/Rakefile +20 -0
- data/lib/vines/storage/sql.rb +220 -0
- data/spec/spec_helper.rb +3 -0
- data/spec/storage/sql_spec.rb +66 -0
- data/spec/storage_specs.rb +182 -0
- data/vines-sql.gemspec +24 -0
- metadata +141 -0
data/Gemfile
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2010-2013 Negative Code
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
|
11
|
+
all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# Welcome to Vines
|
|
2
|
+
|
|
3
|
+
Vines is a scalable XMPP chat server, using EventMachine for asynchronous IO.
|
|
4
|
+
This gem provides support for storing user data in SQL databases with Active
|
|
5
|
+
Record.
|
|
6
|
+
|
|
7
|
+
Additional documentation can be found at [getvines.org](http://www.getvines.org/).
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
$ gem install vines vines-sql
|
|
13
|
+
$ vines init wonderland.lit
|
|
14
|
+
$ cd wonderland.lit && vines start
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Configuration
|
|
18
|
+
|
|
19
|
+
Add the following configuration block to a virtual host definition in
|
|
20
|
+
the server's `conf/config.rb` file.
|
|
21
|
+
|
|
22
|
+
```ruby
|
|
23
|
+
storage 'sql' do
|
|
24
|
+
adapter 'postgresql'
|
|
25
|
+
host 'localhost'
|
|
26
|
+
port 5432
|
|
27
|
+
database 'xmpp'
|
|
28
|
+
username ''
|
|
29
|
+
password ''
|
|
30
|
+
pool 5
|
|
31
|
+
end
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Dependencies
|
|
35
|
+
|
|
36
|
+
Vines requires Ruby 1.9.3 or better. Instructions for installing the
|
|
37
|
+
needed OS packages, as well as Ruby itself, are available at
|
|
38
|
+
[getvines.org/ruby](http://www.getvines.org/ruby).
|
|
39
|
+
|
|
40
|
+
## Development
|
|
41
|
+
|
|
42
|
+
```
|
|
43
|
+
$ script/bootstrap
|
|
44
|
+
$ script/tests
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Contact
|
|
48
|
+
|
|
49
|
+
* David Graham <david@negativecode.com>
|
|
50
|
+
|
|
51
|
+
## License
|
|
52
|
+
|
|
53
|
+
Vines is released under the MIT license. Check the LICENSE file for details.
|
data/Rakefile
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
require 'rake'
|
|
2
|
+
require 'rake/clean'
|
|
3
|
+
require 'rake/testtask'
|
|
4
|
+
|
|
5
|
+
CLOBBER.include('pkg')
|
|
6
|
+
|
|
7
|
+
directory 'pkg'
|
|
8
|
+
|
|
9
|
+
desc 'Build distributable packages'
|
|
10
|
+
task :build => [:pkg] do
|
|
11
|
+
system 'gem build vines-sql.gemspec && mv vines-*.gem pkg/'
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
Rake::TestTask.new(:test) do |test|
|
|
15
|
+
test.libs << 'spec'
|
|
16
|
+
test.pattern = 'spec/**/*_spec.rb'
|
|
17
|
+
test.warning = false
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
task :default => [:clobber, :test, :build]
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
require 'active_record'
|
|
2
|
+
|
|
3
|
+
module Vines
|
|
4
|
+
class Storage
|
|
5
|
+
class Sql < Storage
|
|
6
|
+
register :sql
|
|
7
|
+
|
|
8
|
+
class Contact < ActiveRecord::Base
|
|
9
|
+
belongs_to :user
|
|
10
|
+
end
|
|
11
|
+
class Fragment < ActiveRecord::Base
|
|
12
|
+
belongs_to :user
|
|
13
|
+
end
|
|
14
|
+
class Group < ActiveRecord::Base; end
|
|
15
|
+
class User < ActiveRecord::Base
|
|
16
|
+
has_many :contacts, :dependent => :destroy
|
|
17
|
+
has_many :fragments, :dependent => :delete_all
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Wrap the method with ActiveRecord connection pool logic, so we properly
|
|
21
|
+
# return connections to the pool when we're finished with them. This also
|
|
22
|
+
# defers the original method by pushing it onto the EM thread pool because
|
|
23
|
+
# ActiveRecord uses blocking IO.
|
|
24
|
+
def self.with_connection(method, args={})
|
|
25
|
+
deferrable = args.key?(:defer) ? args[:defer] : true
|
|
26
|
+
old = instance_method(method)
|
|
27
|
+
define_method method do |*args|
|
|
28
|
+
ActiveRecord::Base.connection_pool.with_connection do
|
|
29
|
+
old.bind(self).call(*args)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
defer(method) if deferrable
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
%w[adapter host port database username password pool].each do |name|
|
|
36
|
+
define_method(name) do |*args|
|
|
37
|
+
if args.first
|
|
38
|
+
@config[name.to_sym] = args.first
|
|
39
|
+
else
|
|
40
|
+
@config[name.to_sym]
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def initialize(&block)
|
|
46
|
+
@config = {}
|
|
47
|
+
instance_eval(&block)
|
|
48
|
+
required = [:adapter, :database]
|
|
49
|
+
required << [:host, :port] unless @config[:adapter] == 'sqlite3'
|
|
50
|
+
required.flatten.each {|key| raise "Must provide #{key}" unless @config[key] }
|
|
51
|
+
[:username, :password].each {|key| @config.delete(key) if empty?(@config[key]) }
|
|
52
|
+
establish_connection
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def find_user(jid)
|
|
56
|
+
jid = JID.new(jid).bare.to_s
|
|
57
|
+
return if jid.empty?
|
|
58
|
+
xuser = user_by_jid(jid)
|
|
59
|
+
return Vines::User.new(jid: jid).tap do |user|
|
|
60
|
+
user.name, user.password = xuser.name, xuser.password
|
|
61
|
+
xuser.contacts.each do |contact|
|
|
62
|
+
groups = contact.groups.map {|group| group.name }
|
|
63
|
+
user.roster << Vines::Contact.new(
|
|
64
|
+
jid: contact.jid,
|
|
65
|
+
name: contact.name,
|
|
66
|
+
subscription: contact.subscription,
|
|
67
|
+
ask: contact.ask,
|
|
68
|
+
groups: groups)
|
|
69
|
+
end
|
|
70
|
+
end if xuser
|
|
71
|
+
end
|
|
72
|
+
with_connection :find_user
|
|
73
|
+
|
|
74
|
+
def save_user(user)
|
|
75
|
+
xuser = user_by_jid(user.jid) || Sql::User.new(jid: user.jid.bare.to_s)
|
|
76
|
+
xuser.name = user.name
|
|
77
|
+
xuser.password = user.password
|
|
78
|
+
|
|
79
|
+
# remove deleted contacts from roster
|
|
80
|
+
xuser.contacts.delete(xuser.contacts.select do |contact|
|
|
81
|
+
!user.contact?(contact.jid)
|
|
82
|
+
end)
|
|
83
|
+
|
|
84
|
+
# update contacts
|
|
85
|
+
xuser.contacts.each do |contact|
|
|
86
|
+
fresh = user.contact(contact.jid)
|
|
87
|
+
contact.update_attributes(
|
|
88
|
+
name: fresh.name,
|
|
89
|
+
ask: fresh.ask,
|
|
90
|
+
subscription: fresh.subscription,
|
|
91
|
+
groups: groups(fresh))
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# add new contacts to roster
|
|
95
|
+
jids = xuser.contacts.map {|c| c.jid }
|
|
96
|
+
user.roster.select {|contact| !jids.include?(contact.jid.bare.to_s) }
|
|
97
|
+
.each do |contact|
|
|
98
|
+
xuser.contacts.build(
|
|
99
|
+
user: xuser,
|
|
100
|
+
jid: contact.jid.bare.to_s,
|
|
101
|
+
name: contact.name,
|
|
102
|
+
ask: contact.ask,
|
|
103
|
+
subscription: contact.subscription,
|
|
104
|
+
groups: groups(contact))
|
|
105
|
+
end
|
|
106
|
+
xuser.save
|
|
107
|
+
end
|
|
108
|
+
with_connection :save_user
|
|
109
|
+
|
|
110
|
+
def find_vcard(jid)
|
|
111
|
+
jid = JID.new(jid).bare.to_s
|
|
112
|
+
return if jid.empty?
|
|
113
|
+
if xuser = user_by_jid(jid)
|
|
114
|
+
Nokogiri::XML(xuser.vcard).root rescue nil
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
with_connection :find_vcard
|
|
118
|
+
|
|
119
|
+
def save_vcard(jid, card)
|
|
120
|
+
xuser = user_by_jid(jid)
|
|
121
|
+
if xuser
|
|
122
|
+
xuser.vcard = card.to_xml
|
|
123
|
+
xuser.save
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
with_connection :save_vcard
|
|
127
|
+
|
|
128
|
+
def find_fragment(jid, node)
|
|
129
|
+
jid = JID.new(jid).bare.to_s
|
|
130
|
+
return if jid.empty?
|
|
131
|
+
if fragment = fragment_by_jid(jid, node)
|
|
132
|
+
Nokogiri::XML(fragment.xml).root rescue nil
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
with_connection :find_fragment
|
|
136
|
+
|
|
137
|
+
def save_fragment(jid, node)
|
|
138
|
+
jid = JID.new(jid).bare.to_s
|
|
139
|
+
fragment = fragment_by_jid(jid, node) ||
|
|
140
|
+
Sql::Fragment.new(
|
|
141
|
+
user: user_by_jid(jid),
|
|
142
|
+
root: node.name,
|
|
143
|
+
namespace: node.namespace.href)
|
|
144
|
+
fragment.xml = node.to_xml
|
|
145
|
+
fragment.save
|
|
146
|
+
end
|
|
147
|
+
with_connection :save_fragment
|
|
148
|
+
|
|
149
|
+
# Create the tables and indexes used by this storage engine.
|
|
150
|
+
def create_schema(args={})
|
|
151
|
+
args[:force] ||= false
|
|
152
|
+
|
|
153
|
+
ActiveRecord::Schema.define do
|
|
154
|
+
create_table :users, force: args[:force] do |t|
|
|
155
|
+
t.string :jid, limit: 512, null: false
|
|
156
|
+
t.string :name, limit: 256, null: true
|
|
157
|
+
t.string :password, limit: 256, null: true
|
|
158
|
+
t.text :vcard, null: true
|
|
159
|
+
end
|
|
160
|
+
add_index :users, :jid, unique: true
|
|
161
|
+
|
|
162
|
+
create_table :contacts, force: args[:force] do |t|
|
|
163
|
+
t.integer :user_id, null: false
|
|
164
|
+
t.string :jid, limit: 512, null: false
|
|
165
|
+
t.string :name, limit: 256, null: true
|
|
166
|
+
t.string :ask, limit: 128, null: true
|
|
167
|
+
t.string :subscription, limit: 128, null: false
|
|
168
|
+
end
|
|
169
|
+
add_index :contacts, [:user_id, :jid], unique: true
|
|
170
|
+
|
|
171
|
+
create_table :groups, force: args[:force] do |t|
|
|
172
|
+
t.string :name, limit: 256, null: false
|
|
173
|
+
end
|
|
174
|
+
add_index :groups, :name, unique: true
|
|
175
|
+
|
|
176
|
+
create_table :contacts_groups, id: false, force: args[:force] do |t|
|
|
177
|
+
t.integer :contact_id, null: false
|
|
178
|
+
t.integer :group_id, null: false
|
|
179
|
+
end
|
|
180
|
+
add_index :contacts_groups, [:contact_id, :group_id], unique: true
|
|
181
|
+
|
|
182
|
+
create_table :fragments, force: args[:force] do |t|
|
|
183
|
+
t.integer :user_id, null: false
|
|
184
|
+
t.string :root, limit: 256, null: false
|
|
185
|
+
t.string :namespace, limit: 256, null: false
|
|
186
|
+
t.text :xml, null: false
|
|
187
|
+
end
|
|
188
|
+
add_index :fragments, [:user_id, :root, :namespace], unique: true
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
with_connection :create_schema, defer: false
|
|
192
|
+
|
|
193
|
+
private
|
|
194
|
+
|
|
195
|
+
def establish_connection
|
|
196
|
+
ActiveRecord::Base.logger = Logger.new('/dev/null')
|
|
197
|
+
ActiveRecord::Base.establish_connection(@config)
|
|
198
|
+
# has_and_belongs_to_many requires a connection so configure the
|
|
199
|
+
# associations here rather than in the class definitions above.
|
|
200
|
+
Sql::Contact.has_and_belongs_to_many :groups
|
|
201
|
+
Sql::Group.has_and_belongs_to_many :contacts
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
def user_by_jid(jid)
|
|
205
|
+
jid = JID.new(jid).bare.to_s
|
|
206
|
+
Sql::User.find_by_jid(jid, :include => {:contacts => :groups})
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
def fragment_by_jid(jid, node)
|
|
210
|
+
jid = JID.new(jid).bare.to_s
|
|
211
|
+
clause = 'user_id=(select id from users where jid=?) and root=? and namespace=?'
|
|
212
|
+
Sql::Fragment.where(clause, jid, node.name, node.namespace.href).first
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
def groups(contact)
|
|
216
|
+
contact.groups.map {|name| Sql::Group.find_or_create_by_name(name.strip) }
|
|
217
|
+
end
|
|
218
|
+
end
|
|
219
|
+
end
|
|
220
|
+
end
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Vines::Storage::Sql do
|
|
4
|
+
include StorageSpecs
|
|
5
|
+
|
|
6
|
+
DB_FILE = "./xmpp_testcase.db"
|
|
7
|
+
ActiveRecord::Migration.verbose = false
|
|
8
|
+
|
|
9
|
+
before do
|
|
10
|
+
storage.create_schema(force: true)
|
|
11
|
+
Vines::Storage::Sql::User.new(jid: 'empty@wonderland.lit', name: '', password: '').save
|
|
12
|
+
Vines::Storage::Sql::User.new(jid: 'no_password@wonderland.lit', name: '', password: '').save
|
|
13
|
+
Vines::Storage::Sql::User.new(jid: 'clear_password@wonderland.lit', name: '', password: 'secret').save
|
|
14
|
+
Vines::Storage::Sql::User.new(jid: 'bcrypt_password@wonderland.lit', name: '',
|
|
15
|
+
password: BCrypt::Password.create('secret')).save
|
|
16
|
+
groups = %w[Group1 Group2 Group3 Group4].map do |name|
|
|
17
|
+
Vines::Storage::Sql::Group.find_or_create_by_name(name)
|
|
18
|
+
end
|
|
19
|
+
full = Vines::Storage::Sql::User.new(
|
|
20
|
+
jid: 'full@wonderland.lit',
|
|
21
|
+
name: 'Tester',
|
|
22
|
+
password: BCrypt::Password.create('secret'),
|
|
23
|
+
vcard: vcard.to_xml)
|
|
24
|
+
full.contacts << Vines::Storage::Sql::Contact.new(
|
|
25
|
+
jid: 'contact1@wonderland.lit',
|
|
26
|
+
name: 'Contact1',
|
|
27
|
+
groups: groups[0, 2],
|
|
28
|
+
subscription: 'both')
|
|
29
|
+
full.contacts << Vines::Storage::Sql::Contact.new(
|
|
30
|
+
jid: 'contact2@wonderland.lit',
|
|
31
|
+
name: 'Contact2',
|
|
32
|
+
groups: groups[2, 2],
|
|
33
|
+
subscription: 'both')
|
|
34
|
+
full.save
|
|
35
|
+
|
|
36
|
+
partial = Vines::Storage::Sql::Fragment.new(
|
|
37
|
+
user: full,
|
|
38
|
+
root: 'characters',
|
|
39
|
+
namespace: 'urn:wonderland',
|
|
40
|
+
xml: fragment.to_xml)
|
|
41
|
+
partial.save
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
after do
|
|
45
|
+
File.delete(DB_FILE) if File.exist?(DB_FILE)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def storage
|
|
49
|
+
Vines::Storage::Sql.new { adapter 'sqlite3'; database DB_FILE }
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
describe 'creating a new instance' do
|
|
53
|
+
it 'raises with no arguments' do
|
|
54
|
+
-> { Vines::Storage::Sql.new {} }.must_raise RuntimeError
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
it 'raises with no database' do
|
|
58
|
+
-> { Vines::Storage::Sql.new { adapter 'postgresql' } }.must_raise RuntimeError
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
it 'does not raise with adapter and database' do
|
|
62
|
+
obj = Vines::Storage::Sql.new { adapter 'sqlite3'; database ':memory:' }
|
|
63
|
+
obj.wont_be_nil
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
# Mixin methods for storage implementation test classes. The behavioral
|
|
2
|
+
# tests are the same regardless of implementation so share those methods
|
|
3
|
+
# here.
|
|
4
|
+
module StorageSpecs
|
|
5
|
+
def fragment_id
|
|
6
|
+
Digest::SHA1.hexdigest("characters:urn:wonderland")
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def fragment
|
|
10
|
+
Nokogiri::XML(%q{
|
|
11
|
+
<characters xmlns="urn:wonderland">
|
|
12
|
+
<character>Alice</character>
|
|
13
|
+
</characters>
|
|
14
|
+
}.strip).root
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def vcard
|
|
18
|
+
Nokogiri::XML(%q{
|
|
19
|
+
<vCard xmlns="vcard-temp">
|
|
20
|
+
<FN>Alice in Wonderland</FN>
|
|
21
|
+
</vCard>
|
|
22
|
+
}.strip).root
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def fibered
|
|
26
|
+
EM.run do
|
|
27
|
+
Fiber.new do
|
|
28
|
+
yield
|
|
29
|
+
EM.stop
|
|
30
|
+
end.resume
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def test_authenticate
|
|
35
|
+
fibered do
|
|
36
|
+
db = storage
|
|
37
|
+
db.authenticate(nil, nil).must_be_nil
|
|
38
|
+
db.authenticate(nil, 'secret').must_be_nil
|
|
39
|
+
db.authenticate('bogus', nil).must_be_nil
|
|
40
|
+
db.authenticate('bogus', 'secret').must_be_nil
|
|
41
|
+
db.authenticate('empty@wonderland.lit', 'secret').must_be_nil
|
|
42
|
+
db.authenticate('no_password@wonderland.lit', 'secret').must_be_nil
|
|
43
|
+
db.authenticate('clear_password@wonderland.lit', 'secret').must_be_nil
|
|
44
|
+
|
|
45
|
+
user = db.authenticate('bcrypt_password@wonderland.lit', 'secret')
|
|
46
|
+
user.wont_be_nil
|
|
47
|
+
user.jid.to_s.must_equal 'bcrypt_password@wonderland.lit'
|
|
48
|
+
|
|
49
|
+
user = db.authenticate('full@wonderland.lit', 'secret')
|
|
50
|
+
user.wont_be_nil
|
|
51
|
+
user.name.must_equal 'Tester'
|
|
52
|
+
user.jid.to_s.must_equal 'full@wonderland.lit'
|
|
53
|
+
|
|
54
|
+
user.roster.length.must_equal 2
|
|
55
|
+
user.roster[0].jid.to_s.must_equal 'contact1@wonderland.lit'
|
|
56
|
+
user.roster[0].name.must_equal 'Contact1'
|
|
57
|
+
user.roster[0].groups.length.must_equal 2
|
|
58
|
+
user.roster[0].groups[0].must_equal 'Group1'
|
|
59
|
+
user.roster[0].groups[1].must_equal 'Group2'
|
|
60
|
+
|
|
61
|
+
user.roster[1].jid.to_s.must_equal 'contact2@wonderland.lit'
|
|
62
|
+
user.roster[1].name.must_equal 'Contact2'
|
|
63
|
+
user.roster[1].groups.length.must_equal 2
|
|
64
|
+
user.roster[1].groups[0].must_equal 'Group3'
|
|
65
|
+
user.roster[1].groups[1].must_equal 'Group4'
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def test_find_user
|
|
70
|
+
fibered do
|
|
71
|
+
db = storage
|
|
72
|
+
user = db.find_user(nil)
|
|
73
|
+
user.must_be_nil
|
|
74
|
+
|
|
75
|
+
user = db.find_user('full@wonderland.lit')
|
|
76
|
+
user.wont_be_nil
|
|
77
|
+
user.jid.to_s.must_equal 'full@wonderland.lit'
|
|
78
|
+
|
|
79
|
+
user = db.find_user(Vines::JID.new('full@wonderland.lit'))
|
|
80
|
+
user.wont_be_nil
|
|
81
|
+
user.jid.to_s.must_equal 'full@wonderland.lit'
|
|
82
|
+
|
|
83
|
+
user = db.find_user(Vines::JID.new('full@wonderland.lit/resource'))
|
|
84
|
+
user.wont_be_nil
|
|
85
|
+
user.jid.to_s.must_equal 'full@wonderland.lit'
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def test_save_user
|
|
90
|
+
fibered do
|
|
91
|
+
db = storage
|
|
92
|
+
user = Vines::User.new(
|
|
93
|
+
jid: 'save_user@domain.tld/resource1',
|
|
94
|
+
name: 'Save User',
|
|
95
|
+
password: 'secret')
|
|
96
|
+
user.roster << Vines::Contact.new(
|
|
97
|
+
jid: 'contact1@domain.tld/resource2',
|
|
98
|
+
name: 'Contact 1')
|
|
99
|
+
db.save_user(user)
|
|
100
|
+
user = db.find_user('save_user@domain.tld')
|
|
101
|
+
user.wont_be_nil
|
|
102
|
+
user.jid.to_s.must_equal 'save_user@domain.tld'
|
|
103
|
+
user.name.must_equal 'Save User'
|
|
104
|
+
user.roster.length.must_equal 1
|
|
105
|
+
user.roster[0].jid.to_s.must_equal 'contact1@domain.tld'
|
|
106
|
+
user.roster[0].name.must_equal 'Contact 1'
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def test_find_vcard
|
|
111
|
+
fibered do
|
|
112
|
+
db = storage
|
|
113
|
+
card = db.find_vcard(nil)
|
|
114
|
+
card.must_be_nil
|
|
115
|
+
|
|
116
|
+
card = db.find_vcard('full@wonderland.lit')
|
|
117
|
+
card.wont_be_nil
|
|
118
|
+
card.to_s.must_equal vcard.to_s
|
|
119
|
+
|
|
120
|
+
card = db.find_vcard(Vines::JID.new('full@wonderland.lit'))
|
|
121
|
+
card.wont_be_nil
|
|
122
|
+
card.to_s.must_equal vcard.to_s
|
|
123
|
+
|
|
124
|
+
card = db.find_vcard(Vines::JID.new('full@wonderland.lit/resource'))
|
|
125
|
+
card.wont_be_nil
|
|
126
|
+
card.to_s.must_equal vcard.to_s
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def test_save_vcard
|
|
131
|
+
fibered do
|
|
132
|
+
db = storage
|
|
133
|
+
db.save_user(Vines::User.new(jid: 'save_user@domain.tld'))
|
|
134
|
+
db.save_vcard('save_user@domain.tld/resource1', vcard)
|
|
135
|
+
card = db.find_vcard('save_user@domain.tld')
|
|
136
|
+
card.wont_be_nil
|
|
137
|
+
card.to_s.must_equal vcard.to_s
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def test_find_fragment
|
|
142
|
+
fibered do
|
|
143
|
+
db = storage
|
|
144
|
+
root = Nokogiri::XML(%q{<characters xmlns="urn:wonderland"/>}).root
|
|
145
|
+
bad_name = Nokogiri::XML(%q{<not_characters xmlns="urn:wonderland"/>}).root
|
|
146
|
+
bad_ns = Nokogiri::XML(%q{<characters xmlns="not:wonderland"/>}).root
|
|
147
|
+
|
|
148
|
+
node = db.find_fragment(nil, nil)
|
|
149
|
+
node.must_be_nil
|
|
150
|
+
|
|
151
|
+
node = db.find_fragment('full@wonderland.lit', bad_name)
|
|
152
|
+
node.must_be_nil
|
|
153
|
+
|
|
154
|
+
node = db.find_fragment('full@wonderland.lit', bad_ns)
|
|
155
|
+
node.must_be_nil
|
|
156
|
+
|
|
157
|
+
node = db.find_fragment('full@wonderland.lit', root)
|
|
158
|
+
node.wont_be_nil
|
|
159
|
+
node.to_s.must_equal fragment.to_s
|
|
160
|
+
|
|
161
|
+
node = db.find_fragment(Vines::JID.new('full@wonderland.lit'), root)
|
|
162
|
+
node.wont_be_nil
|
|
163
|
+
node.to_s.must_equal fragment.to_s
|
|
164
|
+
|
|
165
|
+
node = db.find_fragment(Vines::JID.new('full@wonderland.lit/resource'), root)
|
|
166
|
+
node.wont_be_nil
|
|
167
|
+
node.to_s.must_equal fragment.to_s
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
def test_save_fragment
|
|
172
|
+
fibered do
|
|
173
|
+
db = storage
|
|
174
|
+
root = Nokogiri::XML(%q{<characters xmlns="urn:wonderland"/>}).root
|
|
175
|
+
db.save_user(Vines::User.new(jid: 'save_user@domain.tld'))
|
|
176
|
+
db.save_fragment('save_user@domain.tld/resource1', fragment)
|
|
177
|
+
node = db.find_fragment('save_user@domain.tld', root)
|
|
178
|
+
node.wont_be_nil
|
|
179
|
+
node.to_s.must_equal fragment.to_s
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
end
|
data/vines-sql.gemspec
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
Gem::Specification.new do |s|
|
|
2
|
+
s.name = 'vines-sql'
|
|
3
|
+
s.version = '0.1.0'
|
|
4
|
+
s.summary = %q[Provides an SQL storage adapter for the Vines XMPP chat server.]
|
|
5
|
+
s.description = %q[Stores Vines user data in an SQL database.]
|
|
6
|
+
|
|
7
|
+
s.authors = ['David Graham']
|
|
8
|
+
s.email = %w[david@negativecode.com]
|
|
9
|
+
s.homepage = 'http://www.getvines.org'
|
|
10
|
+
s.license = 'MIT'
|
|
11
|
+
|
|
12
|
+
s.files = Dir['[A-Z]*', 'vines-sql.gemspec', 'lib/**/*'] - ['Gemfile.lock']
|
|
13
|
+
s.test_files = Dir['spec/**/*']
|
|
14
|
+
s.require_path = 'lib'
|
|
15
|
+
|
|
16
|
+
s.add_dependency 'activerecord', '~> 3.2.13'
|
|
17
|
+
s.add_dependency 'vines', '>= 0.4.5'
|
|
18
|
+
|
|
19
|
+
s.add_development_dependency 'minitest', '~> 4.7.4'
|
|
20
|
+
s.add_development_dependency 'rake', '~> 10.1.0'
|
|
21
|
+
s.add_development_dependency 'sqlite3', '~> 1.3.7'
|
|
22
|
+
|
|
23
|
+
s.required_ruby_version = '>= 1.9.3'
|
|
24
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: vines-sql
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- David Graham
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2013-06-21 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: activerecord
|
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
|
17
|
+
none: false
|
|
18
|
+
requirements:
|
|
19
|
+
- - ~>
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: 3.2.13
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
none: false
|
|
26
|
+
requirements:
|
|
27
|
+
- - ~>
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: 3.2.13
|
|
30
|
+
- !ruby/object:Gem::Dependency
|
|
31
|
+
name: vines
|
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
|
33
|
+
none: false
|
|
34
|
+
requirements:
|
|
35
|
+
- - ! '>='
|
|
36
|
+
- !ruby/object:Gem::Version
|
|
37
|
+
version: 0.4.5
|
|
38
|
+
type: :runtime
|
|
39
|
+
prerelease: false
|
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
41
|
+
none: false
|
|
42
|
+
requirements:
|
|
43
|
+
- - ! '>='
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: 0.4.5
|
|
46
|
+
- !ruby/object:Gem::Dependency
|
|
47
|
+
name: minitest
|
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
|
49
|
+
none: false
|
|
50
|
+
requirements:
|
|
51
|
+
- - ~>
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: 4.7.4
|
|
54
|
+
type: :development
|
|
55
|
+
prerelease: false
|
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
57
|
+
none: false
|
|
58
|
+
requirements:
|
|
59
|
+
- - ~>
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: 4.7.4
|
|
62
|
+
- !ruby/object:Gem::Dependency
|
|
63
|
+
name: rake
|
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
|
65
|
+
none: false
|
|
66
|
+
requirements:
|
|
67
|
+
- - ~>
|
|
68
|
+
- !ruby/object:Gem::Version
|
|
69
|
+
version: 10.1.0
|
|
70
|
+
type: :development
|
|
71
|
+
prerelease: false
|
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
73
|
+
none: false
|
|
74
|
+
requirements:
|
|
75
|
+
- - ~>
|
|
76
|
+
- !ruby/object:Gem::Version
|
|
77
|
+
version: 10.1.0
|
|
78
|
+
- !ruby/object:Gem::Dependency
|
|
79
|
+
name: sqlite3
|
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
|
81
|
+
none: false
|
|
82
|
+
requirements:
|
|
83
|
+
- - ~>
|
|
84
|
+
- !ruby/object:Gem::Version
|
|
85
|
+
version: 1.3.7
|
|
86
|
+
type: :development
|
|
87
|
+
prerelease: false
|
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
89
|
+
none: false
|
|
90
|
+
requirements:
|
|
91
|
+
- - ~>
|
|
92
|
+
- !ruby/object:Gem::Version
|
|
93
|
+
version: 1.3.7
|
|
94
|
+
description: Stores Vines user data in an SQL database.
|
|
95
|
+
email:
|
|
96
|
+
- david@negativecode.com
|
|
97
|
+
executables: []
|
|
98
|
+
extensions: []
|
|
99
|
+
extra_rdoc_files: []
|
|
100
|
+
files:
|
|
101
|
+
- Gemfile
|
|
102
|
+
- LICENSE
|
|
103
|
+
- Rakefile
|
|
104
|
+
- README.md
|
|
105
|
+
- vines-sql.gemspec
|
|
106
|
+
- lib/vines/storage/sql.rb
|
|
107
|
+
- spec/spec_helper.rb
|
|
108
|
+
- spec/storage/sql_spec.rb
|
|
109
|
+
- spec/storage_specs.rb
|
|
110
|
+
homepage: http://www.getvines.org
|
|
111
|
+
licenses:
|
|
112
|
+
- MIT
|
|
113
|
+
post_install_message:
|
|
114
|
+
rdoc_options: []
|
|
115
|
+
require_paths:
|
|
116
|
+
- lib
|
|
117
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
118
|
+
none: false
|
|
119
|
+
requirements:
|
|
120
|
+
- - ! '>='
|
|
121
|
+
- !ruby/object:Gem::Version
|
|
122
|
+
version: 1.9.3
|
|
123
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
124
|
+
none: false
|
|
125
|
+
requirements:
|
|
126
|
+
- - ! '>='
|
|
127
|
+
- !ruby/object:Gem::Version
|
|
128
|
+
version: '0'
|
|
129
|
+
segments:
|
|
130
|
+
- 0
|
|
131
|
+
hash: 320932205411331743
|
|
132
|
+
requirements: []
|
|
133
|
+
rubyforge_project:
|
|
134
|
+
rubygems_version: 1.8.23
|
|
135
|
+
signing_key:
|
|
136
|
+
specification_version: 3
|
|
137
|
+
summary: Provides an SQL storage adapter for the Vines XMPP chat server.
|
|
138
|
+
test_files:
|
|
139
|
+
- spec/spec_helper.rb
|
|
140
|
+
- spec/storage/sql_spec.rb
|
|
141
|
+
- spec/storage_specs.rb
|