github.com/cozy/cozy-stack@v0.0.0-20240327093429-939e4a21320e/tests/system/lib/model.rb (about)

     1  module Model
     2    module ClassMethods
     3      def create(inst, opts = {})
     4        obj = new opts
     5        obj.save inst
     6        obj
     7      end
     8  
     9      def find(inst, id)
    10        opts = {
    11          accept: :json,
    12          authorization: "Bearer #{inst.token_for doctype}"
    13        }
    14        res = inst.client["/data/#{doctype}/#{id}"].get opts
    15        from_json JSON.parse(res.body)
    16      end
    17  
    18      def all(inst)
    19        opts = {
    20          accept: :json,
    21          authorization: "Bearer #{inst.token_for doctype}"
    22        }
    23        res = inst.client["/data/#{doctype}/_all_docs?include_docs=true"].get opts
    24        JSON.parse(res.body)["rows"]
    25            .reject { |r| r["id"] =~ /^_design/ }
    26            .map { |r| from_json r["doc"] }
    27      end
    28  
    29      def changes(inst, since = nil)
    30        opts = {
    31          accept: :json,
    32          authorization: "Bearer #{inst.token_for doctype}"
    33        }
    34        url = "/data/#{doctype}/_changes?include_docs=true"
    35        url = "#{url}&since=#{since}" if since
    36        res = inst.client[url].get opts
    37        JSON.parse(res.body)
    38      end
    39    end
    40  
    41    def to_json(*_args)
    42      JSON.generate as_json
    43    end
    44  
    45    def as_reference
    46      {
    47        type: doctype,
    48        id: @couch_id
    49      }
    50    end
    51  
    52    def save(inst)
    53      opts = {
    54        content_type: :json,
    55        accept: :json,
    56        authorization: "Bearer #{inst.token_for doctype}"
    57      }
    58      res = if couch_id
    59              inst.client["/data/#{doctype}/#{couch_id}"].put to_json, opts
    60            else
    61              inst.client["/data/#{doctype}/"].post to_json, opts
    62            end
    63      j = JSON.parse(res.body)
    64      @couch_id = j["id"]
    65      @couch_rev = j["rev"]
    66    end
    67  
    68    def delete(inst)
    69      opts = {
    70        accept: "application/vnd.api+json",
    71        content_type: "application/vnd.api+json",
    72        authorization: "Bearer #{inst.token_for doctype}"
    73      }
    74      inst.client["/data/#{doctype}/#{@couch_id}?rev=#{@couch_rev}"].delete opts
    75    end
    76  
    77    def doctype
    78      self.class.doctype
    79    end
    80  
    81    def self.included(klass)
    82      klass.extend ClassMethods
    83      klass.send :attr_accessor, :couch_id, :couch_rev
    84    end
    85  
    86  
    87    module Files
    88      module ClassMethods
    89        def doctype
    90          "io.cozy.files"
    91        end
    92  
    93        def get_id_from_path(inst, path)
    94          find_by_path(inst, path).couch_id
    95        end
    96  
    97        def find_by_path(inst, path)
    98          load_from_url inst, "/files/metadata?Path=#{CGI.escape path}"
    99        end
   100  
   101        def find(inst, id)
   102          load_from_url inst, "/files/#{id}"
   103        end
   104      end
   105  
   106      def self.included(klass)
   107        klass.include Model
   108        klass.extend ClassMethods
   109      end
   110  
   111      def rename(inst, name)
   112        patch inst, name: name
   113        @name = name
   114      end
   115  
   116      def move_to(inst, dir_id)
   117        patch inst, dir_id: dir_id
   118        @dir_id = dir_id
   119      end
   120  
   121      def remove(inst)
   122        opts = {
   123          accept: "application/vnd.api+json",
   124          content_type: "application/vnd.api+json",
   125          authorization: "Bearer #{inst.token_for doctype}"
   126        }
   127        inst.client["/files/#{@couch_id}"].delete opts
   128      end
   129  
   130      def patch(inst, attrs)
   131        body = {
   132          data: {
   133            type: "io.cozy.files",
   134            id: @couch_id,
   135            attributes: attrs
   136          }
   137        }
   138        opts = {
   139          accept: "application/vnd.api+json",
   140          content_type: "application/vnd.api+json",
   141          authorization: "Bearer #{inst.token_for doctype}"
   142        }
   143        res = inst.client["/files/#{@couch_id}"].patch body.to_json, opts
   144        j = JSON.parse(res.body)["data"]
   145        @couch_rev = j["meta"]["rev"]
   146        @cozy_metadata = j["attributes"]["cozyMetadata"]
   147      rescue RestClient::InternalServerError => e
   148        puts "InternalServerError: #{e.http_body}"
   149        raise e
   150      end
   151  
   152      def restore(inst)
   153        restore_from_trash inst
   154      end
   155    end
   156  end