19
19
from dsmr_datalogger .models .reading import DsmrReading
20
20
import dsmr_consumption .services
21
21
from dsmr_frontend .forms import ExportAsCsvForm
22
+ from dsmr_frontend .models .message import Notification
22
23
23
24
24
25
class TestViews (TestCase ):
@@ -88,14 +89,12 @@ def test_dashboard(self, now_mock):
88
89
len (json .loads (response .context ['electricity_y' ])), 0
89
90
)
90
91
91
- self .assertGreater (response .context ['latest_electricity' ], 0 )
92
92
self .assertTrue (response .context ['track_temperature' ])
93
93
self .assertIn ('consumption' , response .context )
94
94
95
95
if self .support_gas :
96
96
self .assertGreater (len (json .loads (response .context ['gas_x' ])), 0 )
97
97
self .assertGreater (len (json .loads (response .context ['gas_y' ])), 0 )
98
- self .assertEqual (response .context ['latest_gas' ], 0 )
99
98
100
99
# Test whether reverse graphs work.
101
100
frontend_settings = FrontendSettings .get_solo ()
@@ -105,6 +104,13 @@ def test_dashboard(self, now_mock):
105
104
response = self .client .get (
106
105
reverse ('{}:dashboard' .format (self .namespace ))
107
106
)
107
+ self .assertEqual (response .status_code , 200 )
108
+
109
+ # XHR views.
110
+ response = self .client .get (
111
+ reverse ('{}:dashboard-xhr-header' .format (self .namespace ))
112
+ )
113
+ self .assertEqual (response .status_code , 200 , response .content )
108
114
109
115
@mock .patch ('django.utils.timezone.now' )
110
116
def test_archive (self , now_mock ):
@@ -120,16 +126,22 @@ def test_archive(self, now_mock):
120
126
'date' : formats .date_format (timezone .now ().date (), 'DSMR_DATEPICKER_DATE_FORMAT' ),
121
127
}
122
128
for current_level in ('days' , 'months' , 'years' ):
123
- data .update ({'level' : current_level })
124
- response = self .client .get (
125
- reverse ('{}:archive-xhr-summary' .format (self .namespace )), data = data
126
- )
127
- self .assertEqual (response .status_code , 200 )
128
-
129
- response = self .client .get (
130
- reverse ('{}:archive-xhr-graphs' .format (self .namespace )), data = data
131
- )
132
- self .assertEqual (response .status_code , 200 , response .content )
129
+ # Test both with tariffs sparated and merged.
130
+ for merge in (False , True ):
131
+ frontend_settings = FrontendSettings .get_solo ()
132
+ frontend_settings .merge_electricity_tariffs = merge
133
+ frontend_settings .save ()
134
+
135
+ data .update ({'level' : current_level })
136
+ response = self .client .get (
137
+ reverse ('{}:archive-xhr-summary' .format (self .namespace )), data = data
138
+ )
139
+ self .assertEqual (response .status_code , 200 )
140
+
141
+ response = self .client .get (
142
+ reverse ('{}:archive-xhr-graphs' .format (self .namespace )), data = data
143
+ )
144
+ self .assertEqual (response .status_code , 200 , response .content )
133
145
134
146
# Invalid XHR.
135
147
data .update ({'level' : 'INVALID DATA' })
@@ -324,15 +336,41 @@ def test_configuration_force_backup(self, now_mock):
324
336
325
337
success_url = reverse ('{}:configuration' .format (self .namespace ))
326
338
self .assertEqual (response .status_code , 302 )
327
- self .assertEqual (
328
- response ['Location' ], 'http://testserver{}' .format (success_url )
329
- )
339
+ self .assertEqual (response ['Location' ], 'http://testserver{}' .format (success_url ))
340
+
330
341
# Setting should have been altered.
331
342
self .assertEqual (
332
343
BackupSettings .get_solo ().latest_backup ,
333
344
now_mock .return_value - timezone .timedelta (days = 7 )
334
345
)
335
346
347
+ def test_notification_read (self ):
348
+ view_url = reverse ('{}:notification-read' .format (self .namespace ))
349
+ notification = Notification .objects .create (message = 'TEST' , redirect_to = 'fake' )
350
+ self .assertFalse (notification .read )
351
+
352
+ # Check login required.
353
+ response = self .client .post (view_url )
354
+ self .assertEqual (response .status_code , 302 )
355
+ self .assertEqual (
356
+ response ['Location' ], 'http://testserver/admin/login/?next={}' .format (view_url )
357
+ )
358
+
359
+ # Login and retest.
360
+ self .client .login (username = 'testuser' , password = 'passwd' )
361
+ response = self .client .post (view_url )
362
+
363
+ response = self .client .post (view_url , data = {'id' : notification .pk })
364
+ self .assertEqual (response .status_code , 302 )
365
+
366
+ # Notification should be altered now.
367
+ self .assertTrue (Notification .objects .get (pk = notification .pk ).read )
368
+
369
+ def test_docs_redirect (self ):
370
+ response = self .client .get (reverse ('{}:docs' .format (self .namespace )))
371
+ self .assertEqual (response .status_code , 302 )
372
+ self .assertTrue (response ['Location' ].startswith ('https://dsmr-reader.readthedocs.io' ))
373
+
336
374
337
375
class TestViewsWithoutData (TestViews ):
338
376
""" Same tests as above, but without any data as it's flushed in setUp(). """
0 commit comments