# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from django import http
from django.urls import reverse

from mox3.mox import IsA

from senlin_dashboard import api
from senlin_dashboard.test import helpers as test

INDEX_URL = reverse('horizon:cluster:receivers:index')
CREATE_URL = reverse('horizon:cluster:receivers:create')


class ReceiversTest(test.TestCase):

    @test.create_stubs({api.senlin: ('receiver_list',)})
    def test_index(self):
        receivers = self.receivers.list()
        api.senlin.receiver_list(
            IsA(http.HttpRequest)).AndReturn(receivers)
        self.mox.ReplayAll()

        res = self.client.get(INDEX_URL)
        self.assertContains(res, '<h1>Receivers</h1>')
        self.assertTemplateUsed(res, 'cluster/receivers/index.html')
        self.assertEqual(1, len(receivers))

    @test.create_stubs({api.senlin: ('receiver_list',)})
    def test_index_receiver_list_exception(self):
        api.senlin.receiver_list(
            IsA(http.HttpRequest)).AndRaise(self.exceptions.senlin)
        self.mox.ReplayAll()

        res = self.client.get(INDEX_URL)
        self.assertTemplateUsed(res, 'cluster/receivers/index.html')
        self.assertEqual(0, len(res.context['receivers_table'].data))
        self.assertMessageCount(res, error=1)

    @test.create_stubs({api.senlin: ('receiver_list',)})
    def test_index_no_receiver(self):
        api.senlin.receiver_list(
            IsA(http.HttpRequest)).AndReturn([])
        self.mox.ReplayAll()

        res = self.client.get(INDEX_URL)
        self.assertTemplateUsed(res, 'cluster/receivers/index.html')
        self.assertContains(res, 'No items to display')
        self.assertEqual(0, len(res.context['receivers_table'].data))

    @test.create_stubs({api.senlin: ('receiver_create',
                                     'cluster_list')})
    def test_create_receiver(self):
        clusters = self.clusters.list()
        receiver = self.receivers.list()[0]
        data = {
            'name': 'test-receiver',
            'type': 'webhook',
            'cluster_id': '123456',
            'action': 'CLUSTER_SCALE_IN',
            'params': ''
        }
        formdata = {
            'name': 'test-receiver',
            'type': 'webhook',
            'cluster_id': '123456',
            'action': 'CLUSTER_SCALE_IN',
            'params': ''
        }

        api.senlin.cluster_list(
            IsA(http.HttpRequest)).AndReturn((clusters, False, False))
        api.senlin.receiver_create(
            IsA(http.HttpRequest), **data).AndReturn(receiver)
        self.mox.ReplayAll()

        res = self.client.post(CREATE_URL, formdata)
        self.assertNoFormErrors(res)

@@ -554,78 +554,28 @@
 ango
- import http%0Afrom django.urls import reverse%0A%0Afrom mox3.mox import IsA
+.urls import reverse
 %0A%0Afr
@@ -816,36 +816,36 @@
    @test.create_
-stub
+mock
 s(%7Bapi.senlin: (
@@ -936,35 +936,34 @@
 t()%0A        
-api.senlin.
+self.mock_
 receiver_lis
@@ -967,94 +967,33 @@
 list
-(%0A            IsA(http.HttpRequest)).AndReturn(receivers)%0A        self.mox.ReplayAll()
+.return_value = receivers
 %0A%0A  
@@ -1198,16 +1198,181 @@
 eivers))
+%0A        self.mock_receiver_list.assert_called_once_with(%0A            test.IsHttpRequest(), filters=%7B%7D, marker=None,%0A            paginate=True, reversed_order=False)
 %0A%0A    @t
@@ -1374,36 +1374,36 @@
    @test.create_
-stub
+mock
 s(%7Bapi.senlin: (
@@ -1476,35 +1476,34 @@
 f):%0A        
-api.senlin.
+self.mock_
 receiver_lis
@@ -1507,105 +1507,59 @@
 list
-(%0A            IsA(http.HttpRequest)).AndRaise(self.exceptions.senlin)%0A        self.mox.ReplayAll(
+.side_effect = (%0A            self.exceptions.senlin
 )%0A%0A 
@@ -1783,38 +1783,203 @@
 rror=1)%0A
-%0A    @test.create_stub
+        self.mock_receiver_list.assert_called_once_with(%0A            test.IsHttpRequest(), filters=%7B%7D, marker=None,%0A            paginate=True, reversed_order=False)%0A%0A    @test.create_mock
 s(%7Bapi.s
@@ -2048,35 +2048,34 @@
 f):%0A        
-api.senlin.
+self.mock_
 receiver_lis
@@ -2079,87 +2079,26 @@
 list
-(%0A            IsA(http.HttpRequest)).AndReturn(%5B%5D)%0A        self.mox.ReplayAll()
+.return_value = %5B%5D
 %0A%0A  
@@ -2330,16 +2330,181 @@
 %5D.data))
+%0A        self.mock_receiver_list.assert_called_once_with(%0A            test.IsHttpRequest(), filters=%7B%7D, marker=None,%0A            paginate=True, reversed_order=False)
 %0A%0A    @t
@@ -2518,12 +2518,12 @@
 ate_
-stub
+mock
 s(%7Ba
@@ -2688,52 +2688,8 @@
 t()%0A
-        receiver = self.receivers.list()%5B0%5D%0A
     
@@ -3093,27 +3093,26 @@
         
-api.senlin.
+self.mock_
 cluster_
@@ -3119,55 +3119,38 @@
 list
-(%0A            IsA(http.HttpRequest)).AndReturn(
+.return_value = (%0A            
 (clu
@@ -3183,19 +3183,18 @@
     
-api.senlin.
+self.mock_
 rece
@@ -3208,101 +3208,28 @@
 eate
-(%0A            IsA(http.HttpRequest), **data).AndReturn(receiver)%0A        self.mox.ReplayAll()
+.return_value = data
 %0A%0A  

Drop mox3 from RecieverList