github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/tests/system/lib/accept.rb (about)

     1  class Accept
     2    def initialize(sharing, sharer = nil)
     3      @sharing = sharing
     4      @owner = @sharing.members.first
     5      @sharer = sharer || @owner
     6    end
     7  
     8    def on(inst)
     9      @inst = inst
    10      state = extract_state_code
    11      location = do_discovery state
    12      sessid = @inst.open_session
    13      click_on_accept state, location, sessid
    14    end
    15  
    16    def extract_state_code
    17      doctype = "io-cozy-sharings"
    18      doc = Couch.new.get_doc @owner.domain, doctype, @sharing.couch_id
    19      idx = doc["members"].find_index { |m| %w(pending mail-not-sent seen).include? m["status"] }
    20      doc["credentials"][idx-1]["state"] if idx
    21    end
    22  
    23    def do_discovery(code)
    24      @sharer.client["/sharings/#{@sharing.couch_id}/discovery?state=#{code}"].get
    25      body = { state: code, url: @inst.url }
    26      res = @sharer.client["/sharings/#{@sharing.couch_id}/discovery"].post body, accept: :json
    27      JSON.parse(res.body)["redirect"]
    28    end
    29  
    30    def click_on_accept(state, location, sessid)
    31      res = RestClient.get location, cookies: { cozysessid: sessid }
    32      csrf_token = res.cookies["_csrf"]
    33      client = RestClient::Resource.new @inst.url
    34      body = {
    35        csrf_token: csrf_token,
    36        state: state,
    37        sharing_id: @sharing.couch_id,
    38        synchronize: "true"
    39      }
    40      params = { cookies: res.cookies, accept: :json }
    41      client["/auth/authorize/sharing"].post body, params
    42    rescue RestClient::Exception
    43      sleep 3
    44      client["/auth/authorize/sharing"].post body, params
    45    end
    46  end