Class: ODDB::TestYusUser

Inherits:
Test::Unit::TestCase
  • Object
show all
Includes:
FlexMock::TestCase
Defined in:
test/test_model/user.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) setup



63
64
65
66
# File 'test/test_model/user.rb', line 63

def setup
  @session = flexmock 'yus-session'
  @user = YusUser.new @session
end

- (Object) test_allowed



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'test/test_model/user.rb', line 67

def test_allowed
  require 'model/activeagent'
  require 'model/company'
  require 'model/fachinfo'
  require 'model/package'
  require 'model/registration'
  require 'model/sequence'

  company = Company.new
  company.pointer = Persistence::Pointer.new [:company, 123]
  comp_privilege = 'org.oddb.model.!company.123'
  @session.should_receive(:allowed?).with('company', comp_privilege).times(1)\
    .and_return do assert true; 'from company' end
  assert_equal 'from company', @user.allowed?('company', company)

  registration = Registration.new '12345'
  registration.pointer = Persistence::Pointer.new [:registration, '12345']
  reg_privilege = 'org.oddb.model.!registration.12345'
  @session.should_receive(:allowed?).with('registration', nil).times(1)\
    .and_return do assert true; false end
  @session.should_receive(:allowed?).with('registration', reg_privilege)\
    .times(1).and_return do assert true; 'from registration' end
  assert_equal 'from registration',
               @user.allowed?('registration', registration)
  registration.company = company
  @session.should_receive(:allowed?).with('registration', comp_privilege)\
    .times(1).and_return do assert true; 'from registration' end
  assert_equal 'from registration',
               @user.allowed?('registration', registration)

  sequence = Sequence.new '01'
  sequence.registration = registration
  @session.should_receive(:allowed?).with('sequence', comp_privilege)\
    .times(1).and_return do assert true; 'from sequence' end
  assert_equal 'from sequence', @user.allowed?('sequence', sequence)

  package = Package.new '001'
  package.sequence = sequence
  @session.should_receive(:allowed?).with('package', comp_privilege)\
    .times(1).and_return do assert true; 'from package' end
  assert_equal 'from package', @user.allowed?('package', package)

  fachinfo = Fachinfo.new
  fachinfo.registrations.push registration
  @session.should_receive(:allowed?).with('fachinfo', comp_privilege)\
    .times(1).and_return do assert true; 'from fachinfo' end
  assert_equal 'from fachinfo', @user.allowed?('fachinfo', fachinfo)

  agent = ActiveAgent.new 'substance'
  agent.sequence = sequence
  @session.should_receive(:allowed?).with('activeagent', comp_privilege)\
    .times(1).and_return do assert true; 'from activeagent' end
  assert_equal 'from activeagent', @user.allowed?('activeagent', agent)
end

- (Object) test_creditable



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'test/test_model/user.rb', line 121

def test_creditable
  require 'model/activeagent'
  require 'model/company'
  require 'model/fachinfo'
  require 'model/package'
  require 'model/registration'
  require 'model/sequence'

  privilege = 'org.oddb.FlexMock'
  @session.should_receive(:allowed?).with('credit', privilege).times(1)\
    .and_return do assert true; 'creditable from class' end
  assert_equal 'creditable from class', @user.creditable?(FlexMock.new)
  privilege = 'org.oddb.Registration'
  @session.should_receive(:allowed?).with('credit', privilege).times(1)\
    .and_return do assert true; 'creditable from string' end
  assert_equal 'creditable from string', @user.creditable?(privilege)
end

- (Object) test_expired



138
139
140
141
# File 'test/test_model/user.rb', line 138

def test_expired
  @session.should_receive(:ping).and_return do assert true; true end
  assert_equal false, @user.expired?
end

- (Object) test_fullname



142
143
144
145
146
147
148
149
150
151
152
# File 'test/test_model/user.rb', line 142

def test_fullname
  @session.should_receive(:get_preference).with(:name_first).and_return do
    assert true
    'FirstName'
  end
  @session.should_receive(:get_preference).with(:name_last).and_return do
    assert true
    'LastName'
  end
  assert_equal 'FirstName LastName', @user.fullname
end

- (Object) test_groups



153
154
155
156
157
158
159
160
# File 'test/test_model/user.rb', line 153

def test_groups
  ent1 = flexmock :name => 'PowerUser'
  ent2 = flexmock :name => 'test@mail.ch'
  ent3 = flexmock :name => 'AdminUser'
  ent4 = flexmock :name => 'other@mail.ch'
  @session.should_receive(:entities).and_return [ent1, ent2, ent3, ent4]
  assert_equal [ent1, ent3], @user.groups
end

- (Object) test_method_missing



161
162
163
164
165
166
167
168
169
170
# File 'test/test_model/user.rb', line 161

def test_method_missing
  block_arg = nil
  @session.should_receive(:something).with('an argument', Proc).times(1)\
    .and_return do |arg, block|
    assert true
    block.call 'a block-argument'
  end
  @user.something('an argument') do |arg| block_arg = arg end
  assert_equal 'a block-argument', block_arg
end

- (Object) test_model



171
172
173
174
175
176
177
178
179
180
# File 'test/test_model/user.rb', line 171

def test_model
  ODBA.cache = flexmock 'odba cache'
  ODBA.cache.should_receive(:fetch).with('an odba id', @user)\
                                   .and_return 'an object'
  @session.should_receive(:get_preference).with('association')\
    .and_return 'an odba id'
  assert_equal 'an object', @user.model
ensure
  ODBA.cache = nil
end

- (Object) test_name



181
182
183
184
185
186
# File 'test/test_model/user.rb', line 181

def test_name
  @session.should_receive(:name).and_return 'test@email.ch'
  assert_equal 'test@email.ch', @user.name
  assert_equal 'test@email.ch', @user.
  assert_equal 'test@email.ch', @user.unique_email
end

- (Object) test_set_preferences



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'test/test_model/user.rb', line 187

def test_set_preferences
  prefs = {
    :salutation    => 'Salutation',
    :name_first    => 'NameFirst',
    :name_last     => 'NameLast',
    :address       => 'Address',
    :city          => 'City',
    :plz           => 'PLZ',
    :company_name  => 'CompanyName',
    :business_area => 'BusinessArea',
    :phone         => 'Phone',
    :poweruser_duration => 'PoweruserDuration',
    :unknown_key   => 'UnknownKey',
  }
  expected = {
    :salutation    => 'Salutation',
    :name_first    => 'NameFirst',
    :name_last     => 'NameLast',
    :address       => 'Address',
    :city          => 'City',
    :plz           => 'PLZ',
    :company_name  => 'CompanyName',
    :business_area => 'BusinessArea',
    :phone         => 'Phone',
    :poweruser_duration => 'PoweruserDuration',
  }
  @session.should_receive(:set_preferences).with(expected).and_return do
    assert true
  end
  @user.set_preferences prefs
end