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

     1  class Rule
     2    attr_reader :title, :doctype, :selector, :values, :add, :update, :remove, :object
     3  
     4    def self.sync(obj)
     5      create_from_obj obj, "sync"
     6    end
     7  
     8    def self.push(obj)
     9      create_from_obj obj, "push"
    10    end
    11  
    12    def self.none(obj)
    13      create_from_obj obj, "none"
    14    end
    15  
    16    def self.create_from_album(obj, what)
    17      selector = "referenced_by"
    18      values = ["#{obj.doctype}/#{obj.couch_id}"]
    19      title = obj.name rescue nil
    20      doctype = "io.cozy.files"
    21      r1 = Rule.new doctype: doctype,
    22                    title: title,
    23                    values: values,
    24                    selector: selector,
    25                    add: what,
    26                    update: what,
    27                    remove: what,
    28                    object: obj
    29      values = [obj.couch_id]
    30      title = obj.name rescue nil
    31      doctype = obj.doctype
    32      r2 = Rule.new doctype: doctype,
    33                    title: title,
    34                    values: values,
    35                    add: what,
    36                    update: what,
    37                    remove: what,
    38                    object: obj
    39      [r1, r2]
    40    end
    41  
    42    def self.create_from_obj(obj, what)
    43      case obj
    44      when Array
    45        values = obj.map(&:couch_id)
    46        doctype = obj.first.doctype
    47        title = obj.first.name rescue nil
    48      else
    49        values = [obj.couch_id]
    50        doctype = obj.doctype
    51        title = obj.name rescue nil
    52      end
    53      Rule.new doctype: doctype,
    54               title: title,
    55               values: values,
    56               add: what,
    57               update: what,
    58               remove: what,
    59               object: obj
    60    end
    61  
    62    def self.create_from_organization(org_id, what)
    63      r1 = Rule.new doctype: Bitwarden::Organization.doctype,
    64                    title: "Organization",
    65                    values: [org_id],
    66                    add: what,
    67                    update: what,
    68                    remove: "revoke"
    69      r2 = Rule.new doctype: Bitwarden::Cipher.doctype,
    70                    title: "Ciphers",
    71                    values: [org_id],
    72                    selector: "organization_id",
    73                    add: what,
    74                    update: what,
    75                    remove: what
    76      [r1, r2]
    77    end
    78  
    79    def initialize(opts = {})
    80      @title = opts[:title] || Faker::Movies::Hobbit.thorins_company
    81      @doctype = opts[:doctype]
    82      @selector = opts[:selector]
    83      @values = opts[:values]
    84      @add = opts[:add]
    85      @update = opts[:update]
    86      @remove = opts[:remove]
    87      @object = opts[:object]
    88    end
    89  
    90    def as_json
    91      {
    92        title: @title,
    93        doctype: @doctype,
    94        selector: @selector,
    95        values: @values,
    96        add: @add,
    97        update: @update,
    98        remove: @remove
    99      }
   100    end
   101  end