Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / admin-interface / tests.py @ c204fcff

History | View | Annotate | Download (14.5 kB)

1 c204fcff Alex Pyrgiotis
# Copyright 2011, 2012, 2013 GRNET S.A. All rights reserved.
2 c204fcff Alex Pyrgiotis
#
3 c204fcff Alex Pyrgiotis
# Redistribution and use in source and binary forms, with or
4 c204fcff Alex Pyrgiotis
# without modification, are permitted provided that the following
5 c204fcff Alex Pyrgiotis
# conditions are met:
6 c204fcff Alex Pyrgiotis
#
7 c204fcff Alex Pyrgiotis
#   1. Redistributions of source code must retain the above
8 c204fcff Alex Pyrgiotis
#      copyright notice, this list of conditions and the following
9 c204fcff Alex Pyrgiotis
#      disclaimer.
10 c204fcff Alex Pyrgiotis
#
11 c204fcff Alex Pyrgiotis
#   2. Redistributions in binary form must reproduce the above
12 c204fcff Alex Pyrgiotis
#      copyright notice, this list of conditions and the following
13 c204fcff Alex Pyrgiotis
#      disclaimer in the documentation and/or other materials
14 c204fcff Alex Pyrgiotis
#      provided with the distribution.
15 c204fcff Alex Pyrgiotis
#
16 c204fcff Alex Pyrgiotis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 c204fcff Alex Pyrgiotis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 c204fcff Alex Pyrgiotis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 c204fcff Alex Pyrgiotis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 c204fcff Alex Pyrgiotis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 c204fcff Alex Pyrgiotis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 c204fcff Alex Pyrgiotis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 c204fcff Alex Pyrgiotis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 c204fcff Alex Pyrgiotis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 c204fcff Alex Pyrgiotis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 c204fcff Alex Pyrgiotis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 c204fcff Alex Pyrgiotis
# POSSIBILITY OF SUCH DAMAGE.
28 c204fcff Alex Pyrgiotis
#
29 c204fcff Alex Pyrgiotis
# The views and conclusions contained in the software and
30 c204fcff Alex Pyrgiotis
# documentation are those of the authors and should not be
31 c204fcff Alex Pyrgiotis
# interpreted as representing official policies, either expressed
32 c204fcff Alex Pyrgiotis
# or implied, of GRNET S.A.
33 c204fcff Alex Pyrgiotis
#
34 c204fcff Alex Pyrgiotis
35 c204fcff Alex Pyrgiotis
import mock
36 c204fcff Alex Pyrgiotis
37 c204fcff Alex Pyrgiotis
from django.test import TestCase, Client
38 c204fcff Alex Pyrgiotis
from django.conf import settings
39 c204fcff Alex Pyrgiotis
from django.core.urlresolvers import reverse
40 c204fcff Alex Pyrgiotis
from snf_django.utils.testing import mocked_quotaholder
41 c204fcff Alex Pyrgiotis
42 c204fcff Alex Pyrgiotis
43 c204fcff Alex Pyrgiotis
USER1 = "5edcb5aa-1111-4146-a8ed-2b6287824353"
44 c204fcff Alex Pyrgiotis
USER2 = "5edcb5aa-2222-4146-a8ed-2b6287824353"
45 c204fcff Alex Pyrgiotis
46 c204fcff Alex Pyrgiotis
USERS_UUIDS = {}
47 c204fcff Alex Pyrgiotis
USERS_UUIDS[USER1] = {'displayname': 'testuser@test.com'}
48 c204fcff Alex Pyrgiotis
USERS_UUIDS[USER2] = {'displayname': 'testuser2@test.com'}
49 c204fcff Alex Pyrgiotis
50 c204fcff Alex Pyrgiotis
USERS_DISPLAYNAMES = dict(map(lambda k: (k[1]['displayname'], {'uuid': k[0]}),
51 c204fcff Alex Pyrgiotis
                          USERS_UUIDS.iteritems()))
52 c204fcff Alex Pyrgiotis
53 c204fcff Alex Pyrgiotis
from synnefo.db import models_factory as mfactory
54 c204fcff Alex Pyrgiotis
55 c204fcff Alex Pyrgiotis
56 c204fcff Alex Pyrgiotis
class AstakosClientMock():
57 c204fcff Alex Pyrgiotis
    def __init__(*args, **kwargs):
58 c204fcff Alex Pyrgiotis
        pass
59 c204fcff Alex Pyrgiotis
60 c204fcff Alex Pyrgiotis
    def get_username(self, uuid):
61 c204fcff Alex Pyrgiotis
        try:
62 c204fcff Alex Pyrgiotis
            return USERS_UUIDS.get(uuid)['displayname']
63 c204fcff Alex Pyrgiotis
        except TypeError:
64 c204fcff Alex Pyrgiotis
            return None
65 c204fcff Alex Pyrgiotis
66 c204fcff Alex Pyrgiotis
    def get_uuid(self, display_name):
67 c204fcff Alex Pyrgiotis
        try:
68 c204fcff Alex Pyrgiotis
            return USERS_DISPLAYNAMES.get(display_name)['uuid']
69 c204fcff Alex Pyrgiotis
        except TypeError:
70 c204fcff Alex Pyrgiotis
            return None
71 c204fcff Alex Pyrgiotis
72 c204fcff Alex Pyrgiotis
73 c204fcff Alex Pyrgiotis
class AuthClient(Client):
74 c204fcff Alex Pyrgiotis
75 c204fcff Alex Pyrgiotis
    def request(self, **request):
76 c204fcff Alex Pyrgiotis
        token = request.pop('user_token', '0000')
77 c204fcff Alex Pyrgiotis
        if token:
78 c204fcff Alex Pyrgiotis
            request['HTTP_X_AUTH_TOKEN'] = token
79 c204fcff Alex Pyrgiotis
        return super(AuthClient, self).request(**request)
80 c204fcff Alex Pyrgiotis
81 c204fcff Alex Pyrgiotis
82 c204fcff Alex Pyrgiotis
def get_user_mock(request, *args, **kwargs):
83 c204fcff Alex Pyrgiotis
    request.user_uniq = None
84 c204fcff Alex Pyrgiotis
    request.user = None
85 c204fcff Alex Pyrgiotis
    if request.META.get('HTTP_X_AUTH_TOKEN', None) == '0000':
86 c204fcff Alex Pyrgiotis
        request.user_uniq = 'test'
87 c204fcff Alex Pyrgiotis
        request.user = {"access": {
88 c204fcff Alex Pyrgiotis
                        "token": {
89 c204fcff Alex Pyrgiotis
                            "expires": "2013-06-19T15:23:59.975572+00:00",
90 c204fcff Alex Pyrgiotis
                            "id": "0000",
91 c204fcff Alex Pyrgiotis
                            "tenant": {
92 c204fcff Alex Pyrgiotis
                                "id": "test",
93 c204fcff Alex Pyrgiotis
                                "name": "Firstname Lastname"
94 c204fcff Alex Pyrgiotis
                                }
95 c204fcff Alex Pyrgiotis
                            },
96 c204fcff Alex Pyrgiotis
                        "serviceCatalog": [],
97 c204fcff Alex Pyrgiotis
                        "user": {
98 c204fcff Alex Pyrgiotis
                            "roles_links": [],
99 c204fcff Alex Pyrgiotis
                            "id": "test",
100 c204fcff Alex Pyrgiotis
                            "roles": [{"id": 1, "name": "default"}],
101 c204fcff Alex Pyrgiotis
                            "name": "Firstname Lastname"}}
102 c204fcff Alex Pyrgiotis
                        }
103 c204fcff Alex Pyrgiotis
104 c204fcff Alex Pyrgiotis
    if request.META.get('HTTP_X_AUTH_TOKEN', None) == '0001':
105 c204fcff Alex Pyrgiotis
        request.user_uniq = 'test'
106 c204fcff Alex Pyrgiotis
        request.user = {"access": {
107 c204fcff Alex Pyrgiotis
                        "token": {
108 c204fcff Alex Pyrgiotis
                            "expires": "2013-06-19T15:23:59.975572+00:00",
109 c204fcff Alex Pyrgiotis
                            "id": "0001",
110 c204fcff Alex Pyrgiotis
                            "tenant": {
111 c204fcff Alex Pyrgiotis
                                "id": "test",
112 c204fcff Alex Pyrgiotis
                                "name": "Firstname Lastname"
113 c204fcff Alex Pyrgiotis
                                }
114 c204fcff Alex Pyrgiotis
                            },
115 c204fcff Alex Pyrgiotis
                        "serviceCatalog": [],
116 c204fcff Alex Pyrgiotis
                        "user": {
117 c204fcff Alex Pyrgiotis
                            "roles_links": [],
118 c204fcff Alex Pyrgiotis
                            "id": "test",
119 c204fcff Alex Pyrgiotis
                            "roles": [{"id": 1, "name": "default"},
120 c204fcff Alex Pyrgiotis
                                      {"id": 2, "name": "admin-interface"}],
121 c204fcff Alex Pyrgiotis
                            "name": "Firstname Lastname"}}
122 c204fcff Alex Pyrgiotis
                        }
123 c204fcff Alex Pyrgiotis
124 c204fcff Alex Pyrgiotis
125 c204fcff Alex Pyrgiotis
@mock.patch("astakosclient.AstakosClient", new=AstakosClientMock)
126 c204fcff Alex Pyrgiotis
@mock.patch("snf_django.lib.astakos.get_user", new=get_user_mock)
127 c204fcff Alex Pyrgiotis
class Admin-InterfaceTests(TestCase):
128 c204fcff Alex Pyrgiotis
    """
129 c204fcff Alex Pyrgiotis
    Admin-Interface tests. Test correctness of permissions and returned data.
130 c204fcff Alex Pyrgiotis
    """
131 c204fcff Alex Pyrgiotis
132 c204fcff Alex Pyrgiotis
    def setUp(self):
133 c204fcff Alex Pyrgiotis
        settings.SKIP_SSH_VALIDATION = True
134 c204fcff Alex Pyrgiotis
        settings.ADMIN-INTERFACE_ENABLED = True
135 c204fcff Alex Pyrgiotis
        self.client = AuthClient()
136 c204fcff Alex Pyrgiotis
137 c204fcff Alex Pyrgiotis
        # init models
138 c204fcff Alex Pyrgiotis
        vm1u1 = mfactory.VirtualMachineFactory(userid=USER1, name="user1 vm",
139 c204fcff Alex Pyrgiotis
                                               pk=1001)
140 c204fcff Alex Pyrgiotis
        vm1u2 = mfactory.VirtualMachineFactory(userid=USER2, name="user2 vm1",
141 c204fcff Alex Pyrgiotis
                                               pk=1002)
142 c204fcff Alex Pyrgiotis
        vm2u2 = mfactory.VirtualMachineFactory(userid=USER2, name="user2 vm2",
143 c204fcff Alex Pyrgiotis
                                               pk=1003)
144 c204fcff Alex Pyrgiotis
145 c204fcff Alex Pyrgiotis
        nic1 = mfactory.NetworkInterfaceFactory(machine=vm1u2,
146 c204fcff Alex Pyrgiotis
                                                userid=vm1u2.userid,
147 c204fcff Alex Pyrgiotis
                                                network__public=False,
148 c204fcff Alex Pyrgiotis
                                                network__userid=USER1)
149 c204fcff Alex Pyrgiotis
        ip2 = mfactory.IPv4AddressFactory(nic__machine=vm1u1,
150 c204fcff Alex Pyrgiotis
                                          userid=vm1u1.userid,
151 c204fcff Alex Pyrgiotis
                                          network__public=True,
152 c204fcff Alex Pyrgiotis
                                          network__userid=None,
153 c204fcff Alex Pyrgiotis
                                          address="195.251.222.211")
154 c204fcff Alex Pyrgiotis
        mfactory.IPAddressLogFactory(address=ip2.address,
155 c204fcff Alex Pyrgiotis
                                     server_id=vm1u1.id,
156 c204fcff Alex Pyrgiotis
                                     network_id=ip2.network.id,
157 c204fcff Alex Pyrgiotis
                                     active=True)
158 c204fcff Alex Pyrgiotis
159 c204fcff Alex Pyrgiotis
    def test_enabled_setting(self):
160 c204fcff Alex Pyrgiotis
        settings.ADMIN-INTERFACE_ENABLED = False
161 c204fcff Alex Pyrgiotis
162 c204fcff Alex Pyrgiotis
        # admin-interface is disabled
163 c204fcff Alex Pyrgiotis
        r = self.client.get(reverse('admin-interface-index'), user_token="0001")
164 c204fcff Alex Pyrgiotis
        self.assertEqual(r.status_code, 404)
165 c204fcff Alex Pyrgiotis
        r = self.client.get(reverse('admin-interface-details',
166 c204fcff Alex Pyrgiotis
                                    args=['testuser@test.com']),
167 c204fcff Alex Pyrgiotis
                            user_token="0001")
168 c204fcff Alex Pyrgiotis
        self.assertEqual(r.status_code, 404)
169 c204fcff Alex Pyrgiotis
170 c204fcff Alex Pyrgiotis
    def test_ip_lookup(self):
171 c204fcff Alex Pyrgiotis
        # ip does not exist, proper message gets displayed
172 c204fcff Alex Pyrgiotis
        r = self.client.get(reverse('admin-interface-details',
173 c204fcff Alex Pyrgiotis
                            args=["195.251.221.122"]), user_token='0001')
174 c204fcff Alex Pyrgiotis
        self.assertFalse(r.context["ip_exists"])
175 c204fcff Alex Pyrgiotis
        self.assertEqual(list(r.context["ips"]), [])
176 c204fcff Alex Pyrgiotis
177 c204fcff Alex Pyrgiotis
        # ip exists
178 c204fcff Alex Pyrgiotis
        r = self.client.get(reverse('admin-interface-details',
179 c204fcff Alex Pyrgiotis
                            args=["195.251.222.211"]), user_token='0001')
180 c204fcff Alex Pyrgiotis
        self.assertTrue(r.context["ip_exists"])
181 c204fcff Alex Pyrgiotis
        ips = r.context["ips"]
182 c204fcff Alex Pyrgiotis
        for ip in ips:
183 c204fcff Alex Pyrgiotis
            self.assertEqual(ip.address, "195.251.222.211")
184 c204fcff Alex Pyrgiotis
185 c204fcff Alex Pyrgiotis
    def test_vm_lookup(self):
186 c204fcff Alex Pyrgiotis
        # vm id does not exist
187 c204fcff Alex Pyrgiotis
        r = self.client.get(reverse('admin-interface-details',
188 c204fcff Alex Pyrgiotis
                            args=["vm-123"]), user_token='0001')
189 c204fcff Alex Pyrgiotis
        self.assertContains(r, 'User with Virtual Machine')
190 c204fcff Alex Pyrgiotis
191 c204fcff Alex Pyrgiotis
        # vm exists, 'test' account discovered
192 c204fcff Alex Pyrgiotis
        r = self.client.get(reverse('admin-interface-details',
193 c204fcff Alex Pyrgiotis
                            args=["vm1001"]), user_token='0001')
194 c204fcff Alex Pyrgiotis
        self.assertEqual(r.context['account'], USER1)
195 c204fcff Alex Pyrgiotis
        r = self.client.get(reverse('admin-interface-details',
196 c204fcff Alex Pyrgiotis
                            args=["vm1002"]), user_token='0001')
197 c204fcff Alex Pyrgiotis
        self.assertEqual(r.context['account'], USER2)
198 c204fcff Alex Pyrgiotis
        # dash also works
199 c204fcff Alex Pyrgiotis
        r = self.client.get(reverse('admin-interface-details',
200 c204fcff Alex Pyrgiotis
                            args=["vm-1002"]), user_token='0001')
201 c204fcff Alex Pyrgiotis
        self.assertEqual(r.context['account'], USER2)
202 c204fcff Alex Pyrgiotis
203 c204fcff Alex Pyrgiotis
    def test_view_permissions(self):
204 c204fcff Alex Pyrgiotis
        # anonymous user gets 403
205 c204fcff Alex Pyrgiotis
        r = self.client.get(reverse('admin-interface-index'), user_token=None)
206 c204fcff Alex Pyrgiotis
        self.assertEqual(r.status_code, 403)
207 c204fcff Alex Pyrgiotis
        r = self.client.get(reverse('admin-interface-details',
208 c204fcff Alex Pyrgiotis
                                    args=['testuser@test.com']),
209 c204fcff Alex Pyrgiotis
                            user_token=None)
210 c204fcff Alex Pyrgiotis
        self.assertEqual(r.status_code, 403)
211 c204fcff Alex Pyrgiotis
212 c204fcff Alex Pyrgiotis
        # user not in admin-interface group gets 403
213 c204fcff Alex Pyrgiotis
        r = self.client.get(reverse('admin-interface-index'))
214 c204fcff Alex Pyrgiotis
        self.assertEqual(r.status_code, 403)
215 c204fcff Alex Pyrgiotis
        r = self.client.get(reverse('admin-interface-details',
216 c204fcff Alex Pyrgiotis
                                    args=['testuser@test.com']))
217 c204fcff Alex Pyrgiotis
        self.assertEqual(r.status_code, 403)
218 c204fcff Alex Pyrgiotis
219 c204fcff Alex Pyrgiotis
        # user with admin-interface group gets 200
220 c204fcff Alex Pyrgiotis
        r = self.client.get(reverse('admin-interface-index'), user_token="0001")
221 c204fcff Alex Pyrgiotis
        self.assertEqual(r.status_code, 200)
222 c204fcff Alex Pyrgiotis
        r = self.client.get(reverse('admin-interface-details',
223 c204fcff Alex Pyrgiotis
                                    args=['testuser@test.com']),
224 c204fcff Alex Pyrgiotis
                            user_token="0001")
225 c204fcff Alex Pyrgiotis
        self.assertEqual(r.status_code, 200)
226 c204fcff Alex Pyrgiotis
227 c204fcff Alex Pyrgiotis
        r = self.client.post(reverse('admin-interface-suspend-vm', args=(1001,)))
228 c204fcff Alex Pyrgiotis
        r = self.client.get(reverse('admin-interface-suspend-vm', args=(1001,)),
229 c204fcff Alex Pyrgiotis
                            user_token="0001", data={'token': '1234'})
230 c204fcff Alex Pyrgiotis
        self.assertEqual(r.status_code, 403)
231 c204fcff Alex Pyrgiotis
        r = self.client.get(reverse('admin-interface-suspend-vm', args=(1001,)),
232 c204fcff Alex Pyrgiotis
                            user_token="0001")
233 c204fcff Alex Pyrgiotis
        self.assertEqual(r.status_code, 403)
234 c204fcff Alex Pyrgiotis
        r = self.client.post(reverse('admin-interface-suspend-vm', args=(1001,)),
235 c204fcff Alex Pyrgiotis
                             user_token="0001", data={'token': '0001'})
236 c204fcff Alex Pyrgiotis
        self.assertEqual(r.status_code, 302)
237 c204fcff Alex Pyrgiotis
        r = self.client.post(reverse('admin-interface-suspend-vm', args=(1001,)),
238 c204fcff Alex Pyrgiotis
                             user_token="0000", data={'token': '0000'})
239 c204fcff Alex Pyrgiotis
        self.assertEqual(r.status_code, 403)
240 c204fcff Alex Pyrgiotis
241 c204fcff Alex Pyrgiotis
    def test_suspend_vm(self):
242 c204fcff Alex Pyrgiotis
        r = self.client.get(reverse('admin-interface-details',
243 c204fcff Alex Pyrgiotis
                                    args=['testuser@test.com']),
244 c204fcff Alex Pyrgiotis
                            user_token="0001")
245 c204fcff Alex Pyrgiotis
        self.assertEqual(r.status_code, 200)
246 c204fcff Alex Pyrgiotis
        vmid = r.context['vms'][0].pk
247 c204fcff Alex Pyrgiotis
        r = self.client.post(reverse('admin-interface-suspend-vm', args=(vmid,)),
248 c204fcff Alex Pyrgiotis
                             data={'token': '0001'}, user_token="0001")
249 c204fcff Alex Pyrgiotis
        self.assertEqual(r.status_code, 302)
250 c204fcff Alex Pyrgiotis
251 c204fcff Alex Pyrgiotis
        r = self.client.get(reverse('admin-interface-details',
252 c204fcff Alex Pyrgiotis
                                    args=['testuser@test.com']),
253 c204fcff Alex Pyrgiotis
                            user_token="0001")
254 c204fcff Alex Pyrgiotis
        self.assertTrue(r.context['vms'][0].suspended)
255 c204fcff Alex Pyrgiotis
256 c204fcff Alex Pyrgiotis
        r = self.client.post(reverse('admin-interface-suspend-vm-release',
257 c204fcff Alex Pyrgiotis
                                     args=(vmid,)), data={'token': '0001'},
258 c204fcff Alex Pyrgiotis
                             user_token="0001")
259 c204fcff Alex Pyrgiotis
        self.assertEqual(r.status_code, 302)
260 c204fcff Alex Pyrgiotis
        r = self.client.get(reverse('admin-interface-details',
261 c204fcff Alex Pyrgiotis
                                    args=['testuser@test.com']),
262 c204fcff Alex Pyrgiotis
                            user_token="0001")
263 c204fcff Alex Pyrgiotis
        self.assertFalse(r.context['vms'][0].suspended)
264 c204fcff Alex Pyrgiotis
265 c204fcff Alex Pyrgiotis
    def test_results_get_filtered(self):
266 c204fcff Alex Pyrgiotis
        """
267 c204fcff Alex Pyrgiotis
        Test that view context data are filtered based on userid provided.
268 c204fcff Alex Pyrgiotis
        Check admin-interface_test.json to see the existing database data.
269 c204fcff Alex Pyrgiotis
        """
270 c204fcff Alex Pyrgiotis
271 c204fcff Alex Pyrgiotis
        # 'testuser@test.com' details, see
272 c204fcff Alex Pyrgiotis
        # helpdes/fixtures/admin-interface_test.json for more details
273 c204fcff Alex Pyrgiotis
        r = self.client.get(reverse('admin-interface-details',
274 c204fcff Alex Pyrgiotis
                                    args=['testuser@test.com']),
275 c204fcff Alex Pyrgiotis
                            user_token="0001")
276 c204fcff Alex Pyrgiotis
        account = r.context['account']
277 c204fcff Alex Pyrgiotis
        vms = r.context['vms']
278 c204fcff Alex Pyrgiotis
        nets = r.context['networks']
279 c204fcff Alex Pyrgiotis
        self.assertEqual(account, USER1)
280 c204fcff Alex Pyrgiotis
        self.assertEqual(vms[0].name, "user1 vm")
281 c204fcff Alex Pyrgiotis
        self.assertEqual(vms.count(), 1)
282 c204fcff Alex Pyrgiotis
        self.assertEqual(len(nets), 2)
283 c204fcff Alex Pyrgiotis
        self.assertEqual(r.context['account_exists'], True)
284 c204fcff Alex Pyrgiotis
285 c204fcff Alex Pyrgiotis
        # 'testuser2@test.com' details, see admin-interface
286 c204fcff Alex Pyrgiotis
        # /fixtures/admin-interface_test.json for more details
287 c204fcff Alex Pyrgiotis
        r = self.client.get(reverse('admin-interface-details',
288 c204fcff Alex Pyrgiotis
                                    args=['testuser2@test.com']),
289 c204fcff Alex Pyrgiotis
                            user_token="0001")
290 c204fcff Alex Pyrgiotis
        account = r.context['account']
291 c204fcff Alex Pyrgiotis
        vms = r.context['vms']
292 c204fcff Alex Pyrgiotis
        nets = r.context['networks']
293 c204fcff Alex Pyrgiotis
        self.assertEqual(account, USER2)
294 c204fcff Alex Pyrgiotis
        self.assertEqual(vms.count(), 2)
295 c204fcff Alex Pyrgiotis
        self.assertEqual(sorted([vms[0].name, vms[1].name]),
296 c204fcff Alex Pyrgiotis
                         sorted(["user2 vm1", "user2 vm2"]))
297 c204fcff Alex Pyrgiotis
        self.assertEqual(len(nets), 0)
298 c204fcff Alex Pyrgiotis
        self.assertEqual(r.context['account_exists'], True)
299 c204fcff Alex Pyrgiotis
300 c204fcff Alex Pyrgiotis
        # 'testuser5@test.com' does not exist, should be redirected to
301 c204fcff Alex Pyrgiotis
        # admin-interface home
302 c204fcff Alex Pyrgiotis
        r = self.client.get(reverse('admin-interface-details',
303 c204fcff Alex Pyrgiotis
                                    args=['testuser5@test.com']),
304 c204fcff Alex Pyrgiotis
                            user_token="0001")
305 c204fcff Alex Pyrgiotis
        vms = r.context['vms']
306 c204fcff Alex Pyrgiotis
        self.assertEqual(r.context['account_exists'], False)
307 c204fcff Alex Pyrgiotis
        self.assertEqual(vms.count(), 0)
308 c204fcff Alex Pyrgiotis
309 c204fcff Alex Pyrgiotis
    def test_start_shutdown(self):
310 c204fcff Alex Pyrgiotis
        from synnefo.logic import backend
311 c204fcff Alex Pyrgiotis
312 c204fcff Alex Pyrgiotis
        self.vm1 = mfactory.VirtualMachineFactory(userid=USER1)
313 c204fcff Alex Pyrgiotis
        pk = self.vm1.pk
314 c204fcff Alex Pyrgiotis
315 c204fcff Alex Pyrgiotis
        r = self.client.post(reverse('admin-interface-vm-shutdown', args=(pk,)))
316 c204fcff Alex Pyrgiotis
        self.assertEqual(r.status_code, 403)
317 c204fcff Alex Pyrgiotis
318 c204fcff Alex Pyrgiotis
        r = self.client.post(reverse('admin-interface-vm-shutdown', args=(pk,)),
319 c204fcff Alex Pyrgiotis
                             data={'token': '0001'})
320 c204fcff Alex Pyrgiotis
        self.assertEqual(r.status_code, 403)
321 c204fcff Alex Pyrgiotis
322 c204fcff Alex Pyrgiotis
        backend.shutdown_instance = shutdown = mock.Mock()
323 c204fcff Alex Pyrgiotis
        shutdown.return_value = 1
324 c204fcff Alex Pyrgiotis
        self.vm1.operstate = 'STARTED'
325 c204fcff Alex Pyrgiotis
        self.vm1.save()
326 c204fcff Alex Pyrgiotis
        with mocked_quotaholder():
327 c204fcff Alex Pyrgiotis
            r = self.client.post(reverse('admin-interface-vm-shutdown', args=(pk,)),
328 c204fcff Alex Pyrgiotis
                                 data={'token': '0001'}, user_token='0001')
329 c204fcff Alex Pyrgiotis
        self.assertEqual(r.status_code, 302)
330 c204fcff Alex Pyrgiotis
        self.assertTrue(shutdown.called)
331 c204fcff Alex Pyrgiotis
        self.assertEqual(len(shutdown.mock_calls), 1)
332 c204fcff Alex Pyrgiotis
333 c204fcff Alex Pyrgiotis
        backend.startup_instance = startup = mock.Mock()
334 c204fcff Alex Pyrgiotis
        startup.return_value = 2
335 c204fcff Alex Pyrgiotis
        self.vm1.operstate = 'STOPPED'
336 c204fcff Alex Pyrgiotis
        self.vm1.save()
337 c204fcff Alex Pyrgiotis
        with mocked_quotaholder():
338 c204fcff Alex Pyrgiotis
            r = self.client.post(reverse('admin-interface-vm-start', args=(pk,)),
339 c204fcff Alex Pyrgiotis
                                 data={'token': '0001'}, user_token='0001')
340 c204fcff Alex Pyrgiotis
        self.assertEqual(r.status_code, 302)
341 c204fcff Alex Pyrgiotis
        self.assertTrue(startup.called)
342 c204fcff Alex Pyrgiotis
        self.assertEqual(len(startup.mock_calls), 1)