Skip to content
Snippets Groups Projects
Commit e8f40257 authored by Bruno Michel's avatar Bruno Michel
Browse files

Accept a sharing

parent 45d728d0
No related branches found
No related tags found
No related merge requests found
...@@ -2,8 +2,12 @@ require 'awesome_print' ...@@ -2,8 +2,12 @@ require 'awesome_print'
require 'faker' require 'faker'
require 'fileutils' require 'fileutils'
require 'json' require 'json'
require 'pry'
require 'rest-client' require 'rest-client'
AwesomePrint.pry!
Pry.config.history.file = File.expand_path "../tmp/.pry_history", __FILE__
base = File.expand_path "..", __FILE__ base = File.expand_path "..", __FILE__
FileUtils.cd base do FileUtils.cd base do
Faker::Config.locale = :fr Faker::Config.locale = :fr
......
#!/usr/bin/env ruby #!/usr/bin/env ruby
require_relative 'boot' require_relative 'boot'
require 'pry'
at_exit { Helpers.cleanup } at_exit { Helpers.cleanup }
Helpers.scenario "interactive" Helpers.scenario "interactive"
Helpers.start_mailhog Helpers.start_mailhog
AwesomePrint.pry!
Pry.config.history.file = File.expand_path "../tmp/.pry_history", __FILE__
Pry.start binding, prompt: Pry::SIMPLE_PROMPT, quiet: true Pry.start binding, prompt: Pry::SIMPLE_PROMPT, quiet: true
class Accept
def initialize(sharing)
@sharing = sharing
@owner = @sharing.members.first
end
def on(inst)
@inst = inst
state = extract_state_code
location = do_discovery state
sessid = connect_to_instance
click_on_accept state, location, sessid
end
def extract_state_code
db = "#{@owner.domain.gsub(/\W/, '-')}%2Fio-cozy-sharings"
doc = Couch.new.get_doc db, @sharing.couch_id
idx = doc["members"].find_index { |m| %w(pending mail-not-sent).include? m["status"] }
doc["credentials"][idx-1]["state"] if idx
end
def do_discovery(code)
body = { state: code, url: "http://#{@inst.domain}/" }
res = @owner.client["/sharings/#{@sharing.couch_id}/discovery"].post body, accept: :json
JSON.parse(res.body)["redirect"]
end
def connect_to_instance
client = RestClient::Resource.new "http://#{@inst.domain}"
res = client["/auth/login"].get
csrf_token = res.cookies["_csrf"]
body = { csrf_token: csrf_token, passphrase: @inst.passphrase }
params = { cookies: res.cookies, accept: :json }
res2 = client["/auth/login"].post body, params
res2.cookies["cozysessid"]
end
def click_on_accept(state, location, sessid)
res = RestClient.get location, cookies: { cozysessid: sessid }
csrf_token = res.cookies["_csrf"]
client = RestClient::Resource.new "http://#{@inst.domain}"
body = { csrf_token: csrf_token, state: state, sharing_id: @sharing.couch_id }
params = { cookies: res.cookies, accept: :json }
client["/auth/authorize/sharing"].post body, params
end
end
...@@ -25,4 +25,8 @@ class Couch ...@@ -25,4 +25,8 @@ class Couch
res = JSON.parse @client["/global%2Finstances/_all_docs"].get(params).body res = JSON.parse @client["/global%2Finstances/_all_docs"].get(params).body
res["rows"].map {|row| row["doc"] } res["rows"].map {|row| row["doc"] }
end end
def get_doc(db, id)
JSON.parse @client["/#{db}/#{id}"].get.body
end
end end
...@@ -18,7 +18,7 @@ class Instance ...@@ -18,7 +18,7 @@ class Instance
@client = RestClient::Resource.new "http://#{@domain}" @client = RestClient::Resource.new "http://#{@domain}"
end end
def url_for(obj = nil) def url(obj = nil)
case obj case obj
when Contact when Contact
@stack.install_app self, "contacts" @stack.install_app self, "contacts"
...@@ -35,11 +35,11 @@ class Instance ...@@ -35,11 +35,11 @@ class Instance
browser = opts[:browser] || ENV['BROWSER'] || 'firefox' browser = opts[:browser] || ENV['BROWSER'] || 'firefox'
case browser case browser
when /firefox/ when /firefox/
`#{browser} -private-window #{url_for obj}` `#{browser} -private-window #{url obj}`
when /chrom/ when /chrom/
`#{browser} --incognito #{url_for obj}` `#{browser} --incognito #{url obj}`
else else
`#{browser} #{url_for obj}` `#{browser} #{url obj}`
end end
end end
...@@ -75,7 +75,7 @@ class Instance ...@@ -75,7 +75,7 @@ class Instance
doc.couch_id = JSON.parse(res.body)["data"]["id"] doc.couch_id = JSON.parse(res.body)["data"]["id"]
end end
def register_sharing(sharing) def register(sharing)
doctypes = sharing.rules.map(&:doctype).uniq doctypes = sharing.rules.map(&:doctype).uniq
token = @stack.token_for self, doctypes token = @stack.token_for self, doctypes
opts = { opts = {
...@@ -87,4 +87,9 @@ class Instance ...@@ -87,4 +87,9 @@ class Instance
res = @client["/sharings/"].post body, opts res = @client["/sharings/"].post body, opts
sharing.couch_id = JSON.parse(res.body)["data"]["id"] sharing.couch_id = JSON.parse(res.body)["data"]["id"]
end end
def accept(sharing)
@stack.install_app self, "drive"
Accept.new(sharing).on self
end
end end
...@@ -3,17 +3,27 @@ ...@@ -3,17 +3,27 @@
require_relative '../boot' require_relative '../boot'
require 'minitest/autorun' require 'minitest/autorun'
describe "When an instance has a folder" do describe "A folder" do
before do Helpers.scenario "push_folder"
@inst = Instance.create name: 'alice' Helpers.start_mailhog
folder = @inst.create_doc Folder.new
contact = @inst.create_doc Contact.new it "can be shared to a recipient in push mode" do
@sharing = Sharing.new # Create the folder
@sharing.rules << Rule.push(folder) inst = Instance.create name: "Alice"
@sharing.members << @inst << contact folder = inst.create_doc Folder.new
end folder.couch_id.wont_be_empty
# Create the sharing
name = "Bob"
contact = inst.create_doc Contact.new givenName: name
sharing = Sharing.new
sharing.rules << Rule.push(folder)
sharing.members << inst << contact
inst.register sharing
it "the folder can be shared (push) to a contact" do # Accept the sharing
@inst.register_sharing @sharing sleep 1
recipient = Instance.create name: name
recipient.accept sharing
end end
end end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment