Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Gelencsér Szabolcs
/
cloud
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
e1ebdb06
authored
Jul 10, 2014
by
Őry Máté
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
dashboard: rewrite renew tests to mocked
parent
405dd1f1
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
193 additions
and
103 deletions
+193
-103
circle/dashboard/tests/test_mockedviews.py
+191
-2
circle/dashboard/tests/test_views.py
+1
-100
circle/dashboard/views.py
+1
-1
No files found.
circle/dashboard/tests/test_mockedviews.py
View file @
e1ebdb06
...
...
@@ -144,7 +144,7 @@ class VmOperationViewTestCase(unittest.TestCase):
view
.
as_view
()(
request
,
pk
=
1234
)
.
render
()
def
test_migrate
(
self
):
request
=
FakeRequestFactory
(
POST
=
{
'node'
:
1
})
request
=
FakeRequestFactory
(
POST
=
{
'node'
:
1
}
,
superuser
=
True
)
view
=
vm_ops
[
'migrate'
]
with
patch
.
object
(
view
,
'get_object'
)
as
go
,
\
...
...
@@ -178,7 +178,24 @@ class VmOperationViewTestCase(unittest.TestCase):
assert
view
.
as_view
()(
request
,
pk
=
1234
)[
'location'
]
assert
msg
.
error
.
called
def
test_migrate_wo_permission
(
self
):
request
=
FakeRequestFactory
(
POST
=
{
'node'
:
1
},
superuser
=
False
)
view
=
vm_ops
[
'migrate'
]
with
patch
.
object
(
view
,
'get_object'
)
as
go
,
\
patch
(
'dashboard.views.get_object_or_404'
)
as
go4
:
inst
=
MagicMock
(
spec
=
Instance
)
inst
.
_meta
.
object_name
=
"Instance"
inst
.
migrate
=
Instance
.
_ops
[
'migrate'
](
inst
)
inst
.
migrate
.
async
=
MagicMock
()
inst
.
has_level
.
return_value
=
True
go
.
return_value
=
inst
go4
.
return_value
=
MagicMock
()
with
self
.
assertRaises
(
PermissionDenied
):
assert
view
.
as_view
()(
request
,
pk
=
1234
)[
'location'
]
def
test_migrate_template
(
self
):
"""check if GET dialog's template can be rendered"""
request
=
FakeRequestFactory
(
superuser
=
True
)
view
=
vm_ops
[
'migrate'
]
...
...
@@ -209,7 +226,8 @@ class VmOperationViewTestCase(unittest.TestCase):
assert
not
msg
.
error
.
called
def
test_save_as_w_name
(
self
):
request
=
FakeRequestFactory
(
POST
=
{
'name'
:
'foobar'
})
request
=
FakeRequestFactory
(
POST
=
{
'name'
:
'foobar'
},
has_perms_mock
=
True
)
view
=
vm_ops
[
'save_as_template'
]
with
patch
.
object
(
view
,
'get_object'
)
as
go
,
\
...
...
@@ -240,6 +258,177 @@ class VmOperationViewTestCase(unittest.TestCase):
self
.
assertEquals
(
rend
.
status_code
,
200
)
class
RenewViewTest
(
unittest
.
TestCase
):
def
test_renew_template
(
self
):
request
=
FakeRequestFactory
(
has_perms_mock
=
True
)
view
=
vm_ops
[
'renew'
]
with
patch
.
object
(
view
,
'get_object'
)
as
go
:
inst
=
MagicMock
(
spec
=
Instance
)
inst
.
_meta
.
object_name
=
"Instance"
inst
.
name
=
'foo'
inst
.
renew
=
Instance
.
_ops
[
'renew'
](
inst
)
inst
.
has_level
.
return_value
=
True
go
.
return_value
=
inst
rend
=
view
.
as_view
()(
request
,
pk
=
1234
)
.
render
()
self
.
assertEquals
(
rend
.
status_code
,
200
)
def
test_renew_by_owner_wo_param
(
self
):
request
=
FakeRequestFactory
(
POST
=
{},
has_perms_mock
=
True
)
view
=
vm_ops
[
'renew'
]
with
patch
.
object
(
view
,
'get_object'
)
as
go
,
\
patch
(
'dashboard.views.get_object_or_404'
)
as
go4
:
inst
=
MagicMock
(
spec
=
Instance
)
inst
.
_meta
.
object_name
=
"Instance"
inst
.
renew
=
Instance
.
_ops
[
'renew'
](
inst
)
inst
.
renew
.
async
=
MagicMock
()
inst
.
has_level
.
return_value
=
True
go
.
return_value
=
inst
go4
.
return_value
=
MagicMock
()
assert
view
.
as_view
()(
request
,
pk
=
1234
)
.
render
()
.
status_code
==
200
# success would redirect
def
test_renew_by_owner_w_param
(
self
):
request
=
FakeRequestFactory
(
POST
=
{
'length'
:
1
},
has_perms_mock
=
True
)
view
=
vm_ops
[
'renew'
]
with
patch
.
object
(
view
,
'get_object'
)
as
go
,
\
patch
(
'dashboard.views.messages'
)
as
msg
,
\
patch
(
'dashboard.views.get_object_or_404'
)
as
go4
:
inst
=
MagicMock
(
spec
=
Instance
)
inst
.
_meta
.
object_name
=
"Instance"
inst
.
renew
=
Instance
.
_ops
[
'renew'
](
inst
)
inst
.
renew
.
async
=
MagicMock
()
inst
.
has_level
.
return_value
=
True
go
.
return_value
=
inst
go4
.
return_value
=
MagicMock
()
assert
view
.
as_view
()(
request
,
pk
=
1234
)
assert
not
msg
.
error
.
called
def
test_renew_get_by_anon_wo_key
(
self
):
request
=
FakeRequestFactory
(
authenticated
=
False
)
view
=
vm_ops
[
'renew'
]
with
patch
.
object
(
view
,
'get_object'
)
as
go
,
\
patch
(
'dashboard.views.get_object_or_404'
)
as
go4
:
inst
=
MagicMock
(
spec
=
Instance
)
inst
.
_meta
.
object_name
=
"Instance"
inst
.
renew
=
Instance
.
_ops
[
'renew'
](
inst
)
inst
.
renew
.
async
=
MagicMock
()
inst
.
has_level
.
return_value
=
False
go
.
return_value
=
inst
go4
.
return_value
=
MagicMock
()
self
.
assertIn
(
'login'
,
view
.
as_view
()(
request
,
pk
=
1234
)[
'location'
])
def
test_renew_get_by_nonowner_wo_key
(
self
):
request
=
FakeRequestFactory
(
has_perms_mock
=
True
)
view
=
vm_ops
[
'renew'
]
with
patch
.
object
(
view
,
'get_object'
)
as
go
,
\
patch
(
'dashboard.views.get_object_or_404'
)
as
go4
:
inst
=
MagicMock
(
spec
=
Instance
)
inst
.
_meta
.
object_name
=
"Instance"
inst
.
renew
=
Instance
.
_ops
[
'renew'
](
inst
)
inst
.
renew
.
async
=
MagicMock
()
inst
.
has_level
.
return_value
=
False
go
.
return_value
=
inst
go4
.
return_value
=
MagicMock
()
with
self
.
assertRaises
(
PermissionDenied
):
assert
view
.
as_view
()(
request
,
pk
=
1234
)
def
test_renew_post_by_nonowner_wo_key
(
self
):
request
=
FakeRequestFactory
(
POST
=
{
'length'
:
1
},
has_perms_mock
=
True
)
view
=
vm_ops
[
'renew'
]
with
patch
.
object
(
view
,
'get_object'
)
as
go
,
\
patch
(
'dashboard.views.get_object_or_404'
)
as
go4
:
inst
=
MagicMock
(
spec
=
Instance
,
pk
=
11
)
inst
.
_meta
.
object_name
=
"Instance"
inst
.
renew
=
Instance
.
_ops
[
'renew'
](
inst
)
inst
.
renew
.
async
=
MagicMock
()
inst
.
has_level
.
return_value
=
False
go
.
return_value
=
inst
go4
.
return_value
=
MagicMock
()
with
self
.
assertRaises
(
PermissionDenied
):
assert
view
.
as_view
()(
request
,
pk
=
1234
)
def
test_renew_get_by_nonowner_w_key
(
self
):
user
=
FakeRequestFactory
(
superuser
=
True
)
.
user
view
=
vm_ops
[
'renew'
]
inst
=
MagicMock
(
spec
=
Instance
,
pk
=
11
)
inst
.
_meta
.
object_name
=
"Instance"
inst
.
renew
=
Instance
.
_ops
[
'renew'
](
inst
)
inst
.
renew
.
async
=
MagicMock
()
key
=
view
.
get_token_url
(
inst
,
user
)
.
split
(
'?'
)[
1
]
.
split
(
'='
)[
1
]
request
=
FakeRequestFactory
(
GET
=
{
'k'
:
key
})
# other user!
with
patch
.
object
(
view
,
'get_object'
)
as
go
,
\
patch
(
'dashboard.views.User.objects'
)
as
gu
,
\
patch
(
'dashboard.views.Lease.get_objects_with_level'
)
as
gol
:
gol
.
return_value
=
views
.
Lease
.
objects
.
all
()
gu
.
get
.
return_value
=
user
go
.
return_value
=
inst
assert
view
.
as_view
()(
request
,
pk
=
1234
)
.
render
()
.
status_code
==
200
def
test_renew_post_by_anon_w_key
(
self
):
user
=
FakeRequestFactory
(
authenticated
=
True
)
.
user
view
=
vm_ops
[
'renew'
]
inst
=
MagicMock
(
spec
=
Instance
,
pk
=
11
)
inst
.
_meta
.
object_name
=
"Instance"
inst
.
renew
=
Instance
.
_ops
[
'renew'
](
inst
)
inst
.
renew
.
async
=
MagicMock
()
inst
.
has_level
=
lambda
user
,
level
:
user
.
is_authenticated
()
key
=
view
.
get_token_url
(
inst
,
user
)
.
split
(
'?'
)[
1
]
.
split
(
'='
)[
1
]
request
=
FakeRequestFactory
(
GET
=
{
'k'
:
key
},
authenticated
=
False
)
with
patch
.
object
(
view
,
'get_object'
)
as
go
,
\
patch
(
'dashboard.views.Lease.get_objects_with_level'
)
as
gol
:
go
.
return_value
=
inst
gol
.
return_value
=
views
.
Lease
.
objects
.
all
()
assert
view
.
as_view
()(
request
,
pk
=
1234
)
.
render
()
.
status_code
==
200
def
test_renew_post_by_anon_w_invalid_key
(
self
):
view
=
vm_ops
[
'renew'
]
key
=
"invalid"
inst
=
MagicMock
(
spec
=
Instance
,
pk
=
11
)
inst
.
_meta
.
object_name
=
"Instance"
inst
.
renew
=
Instance
.
_ops
[
'renew'
](
inst
)
inst
.
renew
.
async
=
MagicMock
()
inst
.
has_level
.
return_value
=
False
request
=
FakeRequestFactory
(
GET
=
{
'k'
:
key
},
authenticated
=
False
)
with
patch
.
object
(
view
,
'get_object'
)
as
go
:
go
.
return_value
=
inst
self
.
assertIn
(
'login'
,
view
.
as_view
()(
request
,
pk
=
1234
)[
'location'
])
def
test_renew_post_by_anon_w_expired_key
(
self
):
def
side
(
max_age
=
None
,
*
args
,
**
kwargs
):
if
max_age
:
raise
views
.
signing
.
BadSignature
user
=
FakeRequestFactory
(
authenticated
=
False
)
.
user
view
=
vm_ops
[
'renew'
]
inst
=
MagicMock
(
spec
=
Instance
,
pk
=
11
)
inst
.
_meta
.
object_name
=
"Instance"
inst
.
renew
=
Instance
.
_ops
[
'renew'
](
inst
)
inst
.
renew
.
async
=
MagicMock
()
inst
.
has_level
.
return_value
=
False
key
=
view
.
get_token_url
(
inst
,
user
)
.
split
(
'?'
)[
1
]
.
split
(
'='
)[
1
]
with
patch
(
'dashboard.views.signing.loads'
)
as
loader
,
\
patch
.
object
(
view
,
'get_object'
)
as
go
:
loader
.
return_value
=
(
inst
.
pk
,
user
.
pk
)
loader
.
side_effect
=
side
request
=
FakeRequestFactory
(
GET
=
{
'k'
:
key
},
user
=
user
)
go
.
return_value
=
inst
self
.
assertIn
(
'login'
,
view
.
as_view
()(
request
,
pk
=
1234
)[
'location'
])
def
FakeRequestFactory
(
user
=
None
,
**
kwargs
):
''' FakeRequestFactory, FakeMessages and FakeRequestContext are good for
mocking out django views; they are MUCH faster than the Django test client.
...
...
circle/dashboard/tests/test_views.py
View file @
e1ebdb06
...
...
@@ -21,14 +21,12 @@ import json
from
django.test
import
TestCase
from
django.test.client
import
Client
from
django.contrib.auth.models
import
User
,
Group
from
django.core.urlresolvers
import
reverse
from
django.contrib.auth.models
import
Permission
from
django.contrib.auth
import
authenticate
from
vm.models
import
Instance
,
InstanceTemplate
,
Lease
,
Node
,
Trait
from
vm.operations
import
WakeUpOperation
from
..models
import
Profile
from
..views
import
VmRenewView
from
storage.models
import
Disk
from
firewall.models
import
Vlan
,
Host
,
VlanGroup
from
mock
import
Mock
,
patch
...
...
@@ -568,10 +566,8 @@ class VmDetailTest(LoginMixin, TestCase):
inst
=
Instance
.
objects
.
get
(
pk
=
1
)
inst
.
manual_state_change
(
'SUSPENDED'
)
inst
.
set_level
(
self
.
u2
,
'user'
)
with
patch
(
'dashboard.views.messages'
)
as
msg
:
response
=
c
.
post
(
"/dashboard/vm/1/op/wake_up/"
)
assert
msg
.
error
.
called
self
.
assertEqual
(
response
.
status_code
,
302
)
self
.
assertEqual
(
response
.
status_code
,
403
)
inst
=
Instance
.
objects
.
get
(
pk
=
1
)
self
.
assertEqual
(
inst
.
status
,
'SUSPENDED'
)
...
...
@@ -1631,101 +1627,6 @@ class TransferOwnershipViewTest(LoginMixin, TestCase):
self
.
assertEquals
(
Instance
.
objects
.
get
(
pk
=
1
)
.
owner
.
pk
,
self
.
u2
.
pk
)
class
RenewViewTest
(
LoginMixin
,
TestCase
):
fixtures
=
[
'test-vm-fixture.json'
]
def
setUp
(
self
):
self
.
u1
=
User
.
objects
.
create
(
username
=
'user1'
)
self
.
u1
.
set_password
(
'password'
)
self
.
u1
.
save
()
Profile
.
objects
.
create
(
user
=
self
.
u1
)
self
.
u2
=
User
.
objects
.
create
(
username
=
'user2'
,
is_staff
=
True
)
self
.
u2
.
set_password
(
'password'
)
self
.
u2
.
save
()
Profile
.
objects
.
create
(
user
=
self
.
u2
)
self
.
us
=
User
.
objects
.
create
(
username
=
'superuser'
,
is_superuser
=
True
)
self
.
us
.
set_password
(
'password'
)
self
.
us
.
save
()
Profile
.
objects
.
create
(
user
=
self
.
us
)
inst
=
Instance
.
objects
.
get
(
pk
=
1
)
inst
.
owner
=
self
.
u1
inst
.
save
()
def
test_renew_by_owner
(
self
):
c
=
Client
()
ct
=
Instance
.
objects
.
get
(
pk
=
1
)
.
activity_log
.
\
filter
(
activity_code__endswith
=
'renew'
)
.
count
()
self
.
login
(
c
,
'user1'
)
response
=
c
.
get
(
'/dashboard/vm/1/renew/'
)
self
.
assertEquals
(
response
.
status_code
,
200
)
response
=
c
.
post
(
'/dashboard/vm/1/renew/'
)
self
.
assertEquals
(
response
.
status_code
,
302
)
ct2
=
Instance
.
objects
.
get
(
pk
=
1
)
.
activity_log
.
\
filter
(
activity_code__endswith
=
'renew'
)
.
count
()
self
.
assertEquals
(
ct
+
1
,
ct2
)
def
test_renew_get_by_nonowner_wo_key
(
self
):
c
=
Client
()
self
.
login
(
c
,
'user2'
)
response
=
c
.
get
(
'/dashboard/vm/1/renew/'
)
self
.
assertEquals
(
response
.
status_code
,
403
)
def
test_renew_post_by_nonowner_wo_key
(
self
):
c
=
Client
()
self
.
login
(
c
,
'user2'
)
response
=
c
.
post
(
'/dashboard/vm/1/renew/'
)
self
.
assertEquals
(
response
.
status_code
,
403
)
def
test_renew_get_by_nonowner_w_key
(
self
):
key
=
VmRenewView
.
get_token_url
(
Instance
.
objects
.
get
(
pk
=
1
),
self
.
u2
)
c
=
Client
()
response
=
c
.
get
(
key
)
self
.
assertEquals
(
response
.
status_code
,
200
)
def
test_renew_post_by_anon_w_key
(
self
):
return
self
.
skipTest
(
"while token operations dont work"
)
key
=
VmRenewView
.
get_token_url
(
Instance
.
objects
.
get
(
pk
=
1
),
self
.
u2
)
ct
=
Instance
.
objects
.
get
(
pk
=
1
)
.
activity_log
.
\
filter
(
activity_code__endswith
=
'renew'
)
.
count
()
c
=
Client
()
response
=
c
.
post
(
key
)
self
.
assertEquals
(
response
.
status_code
,
302
)
ct2
=
Instance
.
objects
.
get
(
pk
=
1
)
.
activity_log
.
\
filter
(
activity_code__endswith
=
'renew'
)
.
count
()
self
.
assertEquals
(
ct
+
1
,
ct2
)
def
test_renew_post_by_anon_w_invalid_key
(
self
):
class
Mockinst
(
object
):
pk
=
2
key
=
VmRenewView
.
get_token_url
(
Mockinst
(),
self
.
u2
)
ct
=
Instance
.
objects
.
get
(
pk
=
1
)
.
activity_log
.
\
filter
(
activity_code__endswith
=
'renew'
)
.
count
()
c
=
Client
()
self
.
login
(
c
,
'user2'
)
response
=
c
.
get
(
key
)
self
.
assertEquals
(
response
.
status_code
,
404
)
response
=
c
.
post
(
key
)
self
.
assertEquals
(
response
.
status_code
,
404
)
ct2
=
Instance
.
objects
.
get
(
pk
=
1
)
.
activity_log
.
\
filter
(
activity_code__endswith
=
'renew'
)
.
count
()
self
.
assertEquals
(
ct
,
ct2
)
def
test_renew_post_by_anon_w_expired_key
(
self
):
key
=
reverse
(
VmRenewView
.
url_name
,
args
=
(
12
,
'WzEyLDFd:1WLbSi:2zIb8SUNAIRIOMTmSmKSSit2gpY'
))
ct
=
Instance
.
objects
.
get
(
pk
=
12
)
.
activity_log
.
\
filter
(
activity_code__endswith
=
'renew'
)
.
count
()
c
=
Client
()
self
.
login
(
c
,
'user2'
)
response
=
c
.
get
(
key
)
self
.
assertEquals
(
response
.
status_code
,
302
)
response
=
c
.
post
(
key
)
self
.
assertEquals
(
response
.
status_code
,
403
)
ct2
=
Instance
.
objects
.
get
(
pk
=
12
)
.
activity_log
.
\
filter
(
activity_code__endswith
=
'renew'
)
.
count
()
self
.
assertEquals
(
ct
,
ct2
)
class
IndexViewTest
(
LoginMixin
,
TestCase
):
fixtures
=
[
'test-vm-fixture.json'
,
'node.json'
]
...
...
circle/dashboard/views.py
View file @
e1ebdb06
...
...
@@ -835,7 +835,7 @@ class VmRenewView(FormOperationMixin, TokenOperationView, VmOperationView):
choices
=
Lease
.
get_objects_with_level
(
"user"
,
self
.
request
.
user
)
default
=
self
.
get_op
()
.
instance
.
lease
if
default
and
default
not
in
choices
:
choices
|=
Lease
.
objects
.
filter
(
pk
=
default
.
pk
)
choices
=
list
(
choices
)
+
[
default
]
val
=
super
(
VmRenewView
,
self
)
.
get_form_kwargs
()
val
.
update
({
'choices'
:
choices
,
'default'
:
default
})
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment