portrait Security and Organization records
[smr.git] / gui / test / integration / demo1_user_session_test.rb
blob0b430aa56ea62298441b812967ae788110acdef2
2 # This file is part of SMR.
4 # SMR is free software: you can redistribute it and/or modify it under the
5 # terms of the GNU General Public License as published by the Free Software
6 # Foundation, either version 3 of the License, or (at your option) any later
7 # version.
9 # SMR is distributed in the hope that it will be useful, but WITHOUT ANY
10 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License along with
14 # SMR.  If not, see <http://www.gnu.org/licenses/>.
16 require 'test_helper'
17 require 'digest/sha1'
18         
19 module Smr  #:nodoc:
20     ##
21     # typical user session doing things except trading positions.
22     class Demo1UserSessionTest < ActionDispatch::IntegrationTest
23         LOGIN = 'demo1'
24         PASSWORD = 'demo'
25     
26         ##
27         # try to login as demo1 user
28         test "login as demo1" do
29             # see if it takes us to the login page
30             get root_path
31             assert_response :redirect, 'accessing / should redirect to /login'
32             follow_redirect!
33             assert_response :success
34             assert_equal login_path, path
35            
36             smr_login(LOGIN, PASSWORD)
37         end
38     
39         ##
40         # try to list documents, upload one, download it again, delete it
41         test "work with documents" do
42             smr_login(LOGIN, PASSWORD)
43     
44             get documents_path
45             assert_response :success
46             assert_not_nil assigns(:portfolios)
47             assert_not_nil assigns(:document)
48             assert_not_nil docs = assigns(:documents)
49             assert docs.is_a?(UploadedFiles) 
50             assert docs.empty?
51     
52             upload_file = fixture_file_upload('quote.yml', 'text/plain')
53             upload_sha1 = Digest::SHA1.hexdigest(upload_file.read)
54     
55             # upload
56             post documents_path,
57                 {:action=>'create', :document=>{:comment=>'IntegrationTest', :content=>upload_file}, :id_portfolio=>'' },
58                 {:html=>{:multipart=>true}, :referer=>documents_path }
59             assert_response :redirect
60             follow_redirect!
61             assert_not_nil docs = assigns(:documents), 'no document shown after upload'
62             assert docs.is_a?(UploadedFiles) 
63             assert_not docs.empty?
64             assert d = docs.each.first
65             assert d.is_a?(UploadedFile)
66     
67             # download nothing
68             get download_path, {}, {'HTTP_REFERER'=>documents_path}
69             assert_redirected_to documents_path
70     
71             # download what we uploaded
72             get download_path, {:id=>d.id}, {'HTTP_REFERER'=>documents_path}
73             assert_equal @response.content_type, 'text/plain'
74             assert_equal Digest::SHA1.hexdigest(@response.body), upload_sha1
75     
76             # delete what we uploaded
77             get delete_document_path, {:id=>d.id}, {'HTTP_REFERER'=>documents_path}
78             assert_redirected_to documents_path
79         end
80     
81         ##
82         # view cashflows from dividends and orders
83         test "view cashflow" do
84             smr_login(LOGIN, PASSWORD)
85     
86             get cashflow_index_path(:timeframe=>:all_time, :portfolio=>0)
87             assert_response :success
88             assert_equal cashflow_index_path, path
89             assert_not_nil log = assigns(:log)
90             assert_not_nil forecast = assigns(:forecast)
92             assert log.is_a?(CashflowLog)
93             assert log.count > 0, 'CashflowLog is empty'
94             log.each do |i|
95                 assert i.is_a?(CashflowItem)
96             end
98             assert forecast.is_a?(CashflowForecast)
99             assert forecast.count > 0, 'CashflowForecast is empty'
100             forecast.each do |i|
101                 assert i.is_a?(CashflowItem)
102             end
103         end
104     
105         ##
106         # view and add FigureData
107         test "work with figure data" do
108             smr_login(LOGIN, PASSWORD)
109     
110             get figures_path
111             assert_response :success
112             assert_equal figures_path, path
113             assert assigns(:securities).is_a?(Array)
114             assert assigns(:selected_security).is_a?(Security)
115             assert assigns(:have_data).is_a?(FalseClass)
116     
117             # select a security that has figure data
118             get figures_path, {:id_security=>25, :show=>'this'}
119             assert_response :success
120             assert assigns(:have_data).is_a?(TrueClass)
121             assert assigns(:datatable).render  # Raises if no data
122     
123             # add new FigureData
124             # - no fixtures on BASF AG, therefore nothing should be shown at first
125             #   and then only FigureData we added from here
126             figure_data = {
127                 :id=>0,             # new
128                 :id_security=>15,      # BASF AG
129                 :id_figure_var=>4,  # Demo1Var1
130                 :period=>'year', :time=>1.days.ago.strftime('%Y-%m-%d'),
131                 :analyst=>'integration test', :value=>1.23, :is_expected=>1,
132                 :is_audited=>1, :comment=>'made by demo1 integration test'
133             }
134     
135             get new_figure_path, :id_security=>figure_data[:id_security], :show=>'this'
136             assert_response :success
137             assert assigns(:figuredata).is_a?(FigureData)
138             assert assigns(:have_data).is_a?(FalseClass)
139             post(figures_path, 
140                 {:action=>'create', :figure_data=>figure_data},
141                 {:referer=>figures_path}
142             )
143             follow_redirect!
144             assert_equal figures_path, path
145             assert assigns(:selected_security).is_a?(Security)
146             assert_equal assigns(:selected_security).id, figure_data[:id_security]
147             assert assigns(:have_data).is_a?(TrueClass)
148     
149             # try again, but store FigureData on a FigureVar that belongs to
150             # another user
151             # note: thats something the Gui does not allow to do
152             figure_data[:id_figure_var] = 1  # FirstVar, belongs :id_user=1 (admin)
153             figure_data[:id_security] = 6       # CocaCola Inc.
154             get new_figure_path, :id_security=>figure_data[:id_security], :show=>'this'
155             assert_response :success
156             assert assigns(:have_data).is_a?(FalseClass)
157             post(figures_path, 
158                 {:action=>'create', :figure_data=>figure_data},
159                 {:referer=>figures_path}
160             )
161             follow_redirect!
162             assert assigns(:have_data).is_a?(FalseClass)
163         end
164     
165         ##
166         # use the personal blog
167         test "read and blog articles" do
168             smr_login(LOGIN, PASSWORD)
169     
170             get blog_index_path
171             assert_response :success
172             assert_equal blog_index_path, path
173             assert_not_nil blog = assigns(:blogroll)
174             assert blog.is_a?(Blog)
175             assert blog.empty?
176     
177             # blog new article
178             get new_blog_path
179             assert_response :success
180             assert_equal new_blog_path, path
181             assert_not_nil article = assigns(:article)
182             assert article.is_a?(Comment)
183             post blog_index_path,
184                 {:action=>'create', :comment=>{:title=>'IntegrationTest Title', :comment=>'IntegrationTest Text'} },
185                 {:referer=>blog_index_path }
186             follow_redirect!
187             assert_equal blog_index_path, path
188             assert_not_nil blog = assigns(:blogroll)
189             assert blog.is_a?(Blog)
190             assert_not blog.empty?
191             item = blog.each.first
192             assert item.is_a?(BlogItem)
193             assert_equal 'Comment', item.type
194             assert_equal 'IntegrationTest Title', item.title
195             assert_equal 'IntegrationTest Text', item.body
196         end
198         ##
199         # use quote records
200         test "work with quoterecords" do
201             smr_login(LOGIN, PASSWORD)
202     
203             get quoterecords_path
204             assert_response :success
206             # get quoterecords on E.On AG (there should not be any) for 2007-08-07
207             post set_session_params_path,
208                 {:smr_browse_date=>{:day=>7, :month=>8, :year=>2007}},
209                 {:referer=>quoterecords_path}
210             follow_redirect!
211             get quoterecords_path, { :id_security=>25, :show=>'this' }
212             assert_response :success
213             assert assigns(:selected_security).is_a?(Security)
214             assert_equal 25, assigns(:selected_security).id
216             assert_not_nil quote = assigns(:intraday_quotes).first
217             assert quote.is_a?(Quote)
219             # post a new quoterecord from :intraday_quotes observations
220             marker_qr=Digest::SHA1.hexdigest(Time.now.to_s)
221             post quoterecords_path,
222                 {:action=>'create',
223                   :quoterecord => {
224                     :id=>'', :id_security=>quote.id_security, :id_quote=>quote.id,
225                     :column=>'upward', :comment=>marker_qr,
226                     :is_pivotal_point=>0, :is_uphit=>0, :is_downhit=>0, :is_signal=>0
227                   },
228                   :commit=>'record this one'
229                 },
230                 {:referer=>quoterecords_path}
231             assert_response :success
232             assert_equal quoterecords_path, path
234             # now we should see that one listed in previous records
235             assert_not_nil qrs = assigns(:quoterecords)
236             assert qrs.is_a?(Smr::Quoterecords)
237             qr = qrs.first
238             assert_equal qr.comment, marker_qr
239             assert_equal qr.column, 'upward'
241             # edit that record
242             get quoterecord_path(:id=>qr.id)
243             assert_response :success
244             assert_not_nil qr_edit = assigns(:quoterecord)
245             assert_equal qr_edit.comment, qr.comment
246             marker_qr_edit=Digest::SHA1.hexdigest(marker_qr + Time.now.to_s)
247             post quoterecords_path,
248                 {:action=>'create',
249                   :quoterecord => {
250                     :id=>qr_edit.id, :column=>'downward', :comment=>marker_qr_edit,
251                     :is_pivotal_point=>0, :is_uphit=>0, :is_downhit=>0, :is_signal=>0
252                   },
253                   :commit=>'record this one'
254                 },
255                 {:referer=>quoterecords_path}
256             assert_response :success
257             assert_not_nil qrs_edited = assigns(:quoterecords)
258             assert_equal qrs_edited.first.comment, marker_qr_edit
259         end
261         ##
262         # record rules on quoterecords
263         test "work with quoterecord rules" do
264             smr_login(LOGIN, PASSWORD)
265     
266             get quoterecord_rules_path
267             assert_response :success
268             assert assigns(:rules).empty?
270             # make new rule
271             marker_rule1=Digest::SHA1.hexdigest('obey!' + Time.now.to_s)
272             get new_quoterecord_rule_path
273             assert_response :success
274             post quoterecord_rules_path,
275                 {   :action=>'create',
276                     :quoterecord_rule=>{:id=>'', :order=>1, :name=>'rule1', :rule=>marker_rule1},
277                     :related_columns=>['secondary_rally', 'downward'],
278                     :commit=>'Save Rule'
279                 },
280                 {:referer=>new_quoterecord_rule_path}
281             follow_redirect!
282             assert_response :success
283             rules = assigns(:rules)
284             assert_not rules.empty?, 'we should have a rule by now'
285             assert_equal rules.first.name, 'rule1'
286             assert_equal rules.first.rule, marker_rule1
287             assert rules.first.get_related_columns.include?('downward'), 'column not related to new rule'
289             # edit our rule
290             marker_edited=Digest::SHA1.hexdigest('I said obey!' + Time.now.to_s)
291             get new_quoterecord_rule_path, :id=>rules.first.id
292             assert_not_nil edit_rule = assigns(:rule)
293             assert_equal edit_rule.rule, marker_rule1
294             post quoterecord_rules_path,
295                 {   :action=>'create',
296                     :quoterecord_rule=>{:id=>edit_rule.id, :order=>1, :name=>edit_rule.name, :rule=>marker_edited},
297                     :related_columns=>['secondary_reaction', 'upward'],
298                     :commit=>'Save Rule'
299                 },
300                 {:referer=>new_quoterecord_rule_path}
301             follow_redirect!
302             assert_response :success
303             assert_not_nil edited_rule = assigns(:rules).first
304             assert_equal edited_rule.name, 'rule1'
305             assert_equal edited_rule.rule, marker_edited
306             assert edited_rule.get_related_columns.include?('upward'), 'edited column relation not present'
307         end
309         ##
310         # add a new portfolio and transfere existing position into it
311         test "add portfolio and transfer position" do
312             smr_login(LOGIN, PASSWORD)
313             get new_objects_portfolio_path
314             assert_response :success
315             assert_not_nil p = assigns(:portfolio)
316             assert p.is_a?(Portfolio)
318             # create portfolio
319             post objects_portfolio_index_path,
320                 {   :action=>'create',
321                     :portfolio=>{:id=>'', :name=>'demo1_testportfolio', :taxallowance=>100, :comment=>'demo1_testcomment'},
322                     :time_created=>'2015-04-08 15:26:58 -0400',
323                     :commit=>'Save Portfolio'
324                 },
325                 {:referer=>new_objects_portfolio_path}
326             follow_redirect!
327             assert_response :success
328             assert_equal objects_portfolio_index_path, path
330             # find & edit existing and new Portfolio
331             existing = true
332             new = true
333             assert_not_nil plist = assigns(:portfolios)
334             plist.each{|p|
335                 if p.name == 'Local Broker' then existing = p else new = p end
336             }
337             assert existing.is_a?(Portfolio), 'existing portfolio no located'
338             assert new.is_a?(Portfolio), 'new portfolio not located'
340             get edit_objects_portfolio_path(:id=>existing.id)
341             assert_response :success
342             assert_not_nil editing = assigns(:portfolio)
343             assert_equal existing.name, editing.name
345             # submit the transfer form, targeting the new portfolio
346             post objects_patch_portfolio_path,
347                 {
348                     :id_portfolio_transfer=>new.id,
349                     :positions=>['14'],              # we just *know* position 14 s;-) (from fixtures)
350                     :commit=>'Transfer Positions'
351                 },
352                 {:referer=>edit_objects_portfolio_path(:id=>existing.id)}
353             follow_redirect!
354             assert_response :success
355             assert_equal objects_portfolio_index_path, path
356             assert_equal 1, Position.where(:id=>14, :id_portfolio=>new.id).count, 'transfered position #14 not in target portfolio'
357         end
359         ##
360         # view an existing order
361         test "view order" do
362             smr_login(LOGIN, PASSWORD)
364             get order_path(:id=>145)
365             assert_response :success
366             assert_equal order_path(:id=>145), path
367             assert_not_nil order = assigns(:order)
368             assert_not_nil documents = assigns(:documents)
370             assert order.is_a?(Order)
371             assert documents.is_a?(Smr::UploadedFiles)
373             get order_path(:id=>999999)
374             assert_response :redirect, 'viewing nonexisting Order does not redirect'
375             follow_redirect!
376             assert_response :success
377             assert_equal root_path, path, 'expected redirect to /'
378         end
380         ##
381         # view report on closed positions and asset statistics
382         test "view report" do
383             smr_login(LOGIN, PASSWORD)
385             get report_index_path(:timeframe=>:all_time, :portfolio=>0)
386             assert_response :success
387             assert_equal report_index_path, path
388             assert_not_nil asset_totals = assigns(:asset_totals)
389             assert_not_nil closed_positions = assigns(:closed_positions)
390             assert_not_nil realized_gain = assigns(:realized_gain)
391             assert_not_nil revenue = assigns(:revenue)
392             #assert_not_nil charges_paid = assigns(:charges_paid)  # disabled, see report controller
393             #assert_not_nil dividend_received = assigns(:dividend_received)
395             assert closed_positions.count > 0, 'No :closed_positions found'
396             closed_positions.each do |p|
397                 assert p.is_a?(Smr::AssetPosition)
398             end
400             assert asset_totals.count > 0, 'No reporting on :asset_totals'
401             asset_totals.each do |t|
402                 assert t.is_a?(Smr::Asset)
403             end
404         end
406         ##
407         # portrait a Security and a Organization
408         test "view portraits" do
409             smr_login(LOGIN, PASSWORD)
411             get portrait_security_path(:id=>106)  # of Country A 5.0% 2000(00)
412             assert_response :success
413             assert_not_nil last_trade = assigns(:last_trade)
414             assert_not_nil security = assigns(:security)
415             assert_not_nil positions = assigns(:positions)
416             assert_not_nil cashflow = assigns(:cashflow)
417             assert last_trade.is_a? Quote
418             assert security.is_a? Security
419             assert_equal 106, security.id, 'portrait on wrong Security'
420             assert positions.first.is_a? Smr::AssetPosition
421             assert cashflow.total > 0
423             get portrait_organization_path(:id=>5)  # of Country A
424             assert_response :success
425             assert_not_nil organization = assigns(:organization)
426             assert_not_nil positions = assigns(:positions)
427             assert_not_nil cashflow = assigns(:cashflow)
428             assert organization.is_a? Organization
429             assert_equal 5, organization.id, 'portrait on wrong Organization'
430             assert positions.first.is_a? Smr::AssetPosition
431             assert cashflow.total > 0
432         end
433     end
435 end # module