parent
976c681157
commit
18930910b8
@ -0,0 +1,46 @@
|
||||
# encoding: utf-8
|
||||
|
||||
# @see DB table 'synchronization_oauths'
|
||||
class SynchronizationOauth < Sequel::Model
|
||||
|
||||
many_to_one :user
|
||||
|
||||
PUBLIC_ATTRIBUTES = [
|
||||
:id,
|
||||
:user_id,
|
||||
:service,
|
||||
:token
|
||||
]
|
||||
|
||||
def public_values
|
||||
Hash[PUBLIC_ATTRIBUTES.map{ |k| [k, (self.send(k) rescue self[k].to_s)] }]
|
||||
end #public_values
|
||||
|
||||
def validate
|
||||
super
|
||||
|
||||
if new?
|
||||
existing_oauth = SynchronizationOauth.filter(
|
||||
user_id: user_id,
|
||||
service: service
|
||||
).first
|
||||
errors.add(:user_id, " already has an oauth token created for service #{:service}") unless existing_oauth.nil?
|
||||
else
|
||||
existing_oauth = SynchronizationOauth.filter(
|
||||
id: id
|
||||
).first
|
||||
errors.add(:id, ' cannot change user or service, only token') unless
|
||||
(existing_oauth.service == service && existing_oauth.user_id == user_id)
|
||||
end
|
||||
end #validate
|
||||
|
||||
def before_save
|
||||
super
|
||||
self.updated_at = Time.now
|
||||
end #before_save
|
||||
|
||||
def ==(oauth_object)
|
||||
self.id == oauth_object.id
|
||||
end #==
|
||||
|
||||
end
|
@ -0,0 +1,16 @@
|
||||
Sequel.migration do
|
||||
up do
|
||||
create_table :synchronization_oauths do
|
||||
primary_key :id
|
||||
Uuid :user_id, index: true
|
||||
Text :service, index: true
|
||||
Text :token
|
||||
DateTime :created_at, default: Sequel::CURRENT_TIMESTAMP
|
||||
DateTime :updated_at, default: Sequel::CURRENT_TIMESTAMP
|
||||
end
|
||||
end
|
||||
|
||||
down do
|
||||
drop_table :synchronization_oauths
|
||||
end
|
||||
end
|
@ -0,0 +1,107 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe SynchronizationOauth do
|
||||
|
||||
before(:all) do
|
||||
@user = create_user(:quota_in_bytes => 500.megabytes, :table_quota => 500)
|
||||
end
|
||||
|
||||
after(:all) do
|
||||
@user.destroy
|
||||
end
|
||||
|
||||
before(:each) do
|
||||
end
|
||||
|
||||
after(:each) do
|
||||
@user.synchronization_oauths.map &:destroy
|
||||
@user.reload
|
||||
end
|
||||
|
||||
context '#creation_updating' do
|
||||
it 'Tests basic creation operations' do
|
||||
service_name = 'testtest'
|
||||
token_value = 'qv2345q235erfaweerfdsdfsds'
|
||||
token_value_2 = 'aaaaaaaaaaaaaaaaaaaaaaaa'
|
||||
|
||||
SynchronizationOauth.all.size.should eq 0
|
||||
|
||||
oauth_entry = SynchronizationOauth.create(
|
||||
user_id: @user.id,
|
||||
service: service_name,
|
||||
token: token_value
|
||||
).reload
|
||||
|
||||
oauth_entry.nil?.should eq false
|
||||
oauth_entry.user_id.should eq @user.id
|
||||
oauth_entry.service.should eq service_name
|
||||
oauth_entry.token.should eq token_value
|
||||
|
||||
SynchronizationOauth.all.size.should eq 1
|
||||
|
||||
entry_second_handle = SynchronizationOauth.where(user_id: @user.id, service: service_name).first
|
||||
entry_second_handle.nil?.should eq false
|
||||
entry_second_handle.user_id.should eq @user.id
|
||||
entry_second_handle.service.should eq service_name
|
||||
entry_second_handle.token.should eq token_value
|
||||
|
||||
entry_second_handle.should eq oauth_entry
|
||||
|
||||
oauth_entry.token = token_value_2
|
||||
oauth_entry.save.reload
|
||||
oauth_entry.token.should eq token_value_2
|
||||
|
||||
expect {
|
||||
oauth_entry.service = 'another_service_name'
|
||||
oauth_entry.save
|
||||
}.to raise_exception Sequel::ValidationFailed
|
||||
oauth_entry.service = service_name
|
||||
|
||||
expect {
|
||||
oauth_entry.user_id = UUIDTools::UUID.timestamp_create
|
||||
oauth_entry.save
|
||||
}.to raise_exception Sequel::ValidationFailed
|
||||
|
||||
expect {
|
||||
SynchronizationOauth.create(
|
||||
user_id: @user.id,
|
||||
service: service_name,
|
||||
token: UUIDTools::UUID.timestamp_create
|
||||
)
|
||||
}.to raise_exception Sequel::ValidationFailed
|
||||
end
|
||||
end
|
||||
|
||||
context '#user_oauths' do
|
||||
it 'tests the one to many association of user and sync oauths' do
|
||||
service_name = 'testtest'
|
||||
token_value = 'qv2345q235erfaweerfdsdfsds'
|
||||
|
||||
@user.synchronization_oauths.size.should eq 0
|
||||
|
||||
SynchronizationOauth.create(
|
||||
user_id: @user.id,
|
||||
service: service_name,
|
||||
token: token_value
|
||||
)
|
||||
@user.reload
|
||||
@user.synchronization_oauths.size.should eq 1
|
||||
|
||||
retrieved_entry = @user.synchronization_oauths.first
|
||||
@user.synchronization_oauths.first.should eq retrieved_entry
|
||||
|
||||
@user.synchronization_oauths.first.destroy
|
||||
@user.reload
|
||||
@user.synchronization_oauths.size.should eq 0
|
||||
|
||||
@user.add_synchronization_oauth(service: service_name, token: token_value)
|
||||
@user.synchronization_oauths.size.should eq 1
|
||||
|
||||
expect {
|
||||
@user.add_synchronization_oauth(service: service_name, token: token_value)
|
||||
}.to raise_exception Sequel::ValidationFailed
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
end
|
Loading…
Reference in new issue