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
01eb957e
authored
Mar 15, 2014
by
Bach Dániel
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'issue-98'
Conflicts: circle/dashboard/tests/test_views.py
parents
33415e82
f9fa1c96
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
203 additions
and
7 deletions
+203
-7
circle/dashboard/fixtures/test-vm-fixture.json
+9
-0
circle/dashboard/tests/test_views.py
+180
-4
circle/dashboard/views.py
+14
-3
No files found.
circle/dashboard/fixtures/test-vm-fixture.json
View file @
01eb957e
...
...
@@ -1231,6 +1231,15 @@
}
},
{
"pk"
:
1366
,
"model"
:
"auth.permission"
,
"fields"
:
{
"codename"
:
"config_ports"
,
"name"
:
"Can configure port forwards."
,
"content_type"
:
28
}
},
{
"pk"
:
1
,
"model"
:
"auth.group"
,
"fields"
:
{
...
...
circle/dashboard/tests/test_views.py
View file @
01eb957e
...
...
@@ -3,14 +3,18 @@ from django.test.client import Client
from
django.contrib.auth.models
import
User
,
Group
from
django.core.exceptions
import
SuspiciousOperation
from
django.core.urlresolvers
import
reverse
from
django.contrib.auth.models
import
Permission
from
vm.models
import
Instance
,
InstanceTemplate
,
Lease
,
Node
from
..models
import
Profile
from
..views
import
VmRenewView
from
storage.models
import
Disk
from
firewall.models
import
Vlan
from
firewall.models
import
Vlan
,
Host
,
VlanGroup
from
mock
import
Mock
import
django.conf
settings
=
django
.
conf
.
settings
.
FIREWALL_SETTINGS
class
LoginMixin
(
object
):
def
login
(
self
,
client
,
username
,
password
=
'password'
):
...
...
@@ -37,6 +41,8 @@ class VmDetailTest(LoginMixin, TestCase):
self
.
g1
.
user_set
.
add
(
self
.
u1
)
self
.
g1
.
user_set
.
add
(
self
.
u2
)
self
.
g1
.
save
()
settings
[
"default_vlangroup"
]
=
'public'
VlanGroup
.
objects
.
create
(
name
=
'public'
)
def
tearDown
(
self
):
super
(
VmDetailTest
,
self
)
.
tearDown
()
...
...
@@ -136,13 +142,19 @@ class VmDetailTest(LoginMixin, TestCase):
password
=
inst
.
pw
response
=
c
.
post
(
"/dashboard/vm/1/"
,
{
'change_password'
:
True
})
self
.
assertEqual
(
response
.
status_code
,
403
)
self
.
assertEqual
(
password
,
inst
.
pw
)
self
.
assertEqual
(
password
,
Instance
.
objects
.
get
(
pk
=
1
)
.
pw
)
def
test_unpermitted_network_add
(
self
):
def
test_unpermitted_network_add_wo_perm
(
self
):
c
=
Client
()
self
.
login
(
c
,
"user2"
)
response
=
c
.
post
(
"/dashboard/vm/1/"
,
{
'new_network_vlan'
:
1
})
self
.
assertEqual
(
response
.
status_code
,
403
)
def
test_unpermitted_network_add_wo_vlan_perm
(
self
):
c
=
Client
()
self
.
login
(
c
,
"user2"
)
inst
=
Instance
.
objects
.
get
(
pk
=
1
)
inst
.
set_level
(
self
.
u
1
,
'owner'
)
inst
.
set_level
(
self
.
u
2
,
'owner'
)
response
=
c
.
post
(
"/dashboard/vm/1/"
,
{
'new_network_vlan'
:
1
})
self
.
assertEqual
(
response
.
status_code
,
403
)
...
...
@@ -304,6 +316,170 @@ class VmDetailTest(LoginMixin, TestCase):
response
=
c
.
get
(
"/dashboard/vm/1/activity/"
)
self
.
assertEqual
(
response
.
status_code
,
200
)
def
test_unpermitted_add_port_wo_config_ports_perm
(
self
):
c
=
Client
()
self
.
login
(
c
,
"user2"
)
inst
=
Instance
.
objects
.
get
(
pk
=
1
)
inst
.
set_level
(
self
.
u2
,
'owner'
)
response
=
c
.
post
(
"/dashboard/vm/1/"
,
{
'port'
:
True
,
'proto'
:
'tcp'
,
'port'
:
'1337'
})
self
.
assertEqual
(
response
.
status_code
,
403
)
def
test_unpermitted_add_port_wo_obj_levels
(
self
):
c
=
Client
()
self
.
login
(
c
,
"user2"
)
self
.
u2
.
user_permissions
.
add
(
Permission
.
objects
.
get
(
name
=
'Can configure port forwards.'
))
response
=
c
.
post
(
"/dashboard/vm/1/"
,
{
'port'
:
True
,
'proto'
:
'tcp'
,
'port'
:
'1337'
})
self
.
assertEqual
(
response
.
status_code
,
403
)
def
test_unpermitted_add_port_w_bad_host
(
self
):
c
=
Client
()
self
.
login
(
c
,
"user2"
)
inst
=
Instance
.
objects
.
get
(
pk
=
1
)
inst
.
set_level
(
self
.
u2
,
'owner'
)
self
.
u2
.
user_permissions
.
add
(
Permission
.
objects
.
get
(
name
=
'Can configure port forwards.'
))
response
=
c
.
post
(
"/dashboard/vm/1/"
,
{
'proto'
:
'tcp'
,
'host_pk'
:
'9999'
,
'port'
:
'1337'
})
self
.
assertEqual
(
response
.
status_code
,
403
)
def
test_permitted_add_port_w_unhandled_exception
(
self
):
c
=
Client
()
self
.
login
(
c
,
"user2"
)
inst
=
Instance
.
objects
.
get
(
pk
=
1
)
inst
.
set_level
(
self
.
u2
,
'owner'
)
vlan
=
Vlan
.
objects
.
get
(
id
=
1
)
vlan
.
set_level
(
self
.
u2
,
'user'
)
response
=
c
.
post
(
"/dashboard/vm/1/"
,
{
'new_network_vlan'
:
1
})
host
=
Host
.
objects
.
get
(
interface__in
=
inst
.
interface_set
.
all
())
self
.
u2
.
user_permissions
.
add
(
Permission
.
objects
.
get
(
name
=
'Can configure port forwards.'
))
port_count
=
len
(
host
.
list_ports
())
response
=
c
.
post
(
"/dashboard/vm/1/"
,
{
'proto'
:
'tcp'
,
'host_pk'
:
host
.
pk
,
'port'
:
'invalid_port'
})
self
.
assertEqual
(
response
.
status_code
,
302
)
self
.
assertEqual
(
len
(
host
.
list_ports
()),
port_count
)
def
test_permitted_add_port
(
self
):
c
=
Client
()
self
.
login
(
c
,
"user2"
)
inst
=
Instance
.
objects
.
get
(
pk
=
1
)
inst
.
set_level
(
self
.
u2
,
'owner'
)
vlan
=
Vlan
.
objects
.
get
(
id
=
1
)
vlan
.
set_level
(
self
.
u2
,
'user'
)
response
=
c
.
post
(
"/dashboard/vm/1/"
,
{
'new_network_vlan'
:
1
})
host
=
Host
.
objects
.
get
(
interface__in
=
inst
.
interface_set
.
all
())
self
.
u2
.
user_permissions
.
add
(
Permission
.
objects
.
get
(
name
=
'Can configure port forwards.'
))
port_count
=
len
(
host
.
list_ports
())
response
=
c
.
post
(
"/dashboard/vm/1/"
,
{
'proto'
:
'tcp'
,
'host_pk'
:
host
.
pk
,
'port'
:
'1337'
})
self
.
assertEqual
(
response
.
status_code
,
302
)
self
.
assertEqual
(
len
(
host
.
list_ports
()),
port_count
+
1
)
def
test_unpermitted_add_tag
(
self
):
c
=
Client
()
self
.
login
(
c
,
"user2"
)
inst
=
Instance
.
objects
.
get
(
pk
=
1
)
inst
.
set_level
(
self
.
u2
,
'user'
)
response
=
c
.
post
(
"/dashboard/vm/1/"
,
{
'new_tag'
:
'test1'
})
self
.
assertEqual
(
response
.
status_code
,
403
)
def
test_permitted_add_tag
(
self
):
c
=
Client
()
self
.
login
(
c
,
"user2"
)
inst
=
Instance
.
objects
.
get
(
pk
=
1
)
inst
.
set_level
(
self
.
u2
,
'owner'
)
tag_count
=
inst
.
tags
.
count
()
response
=
c
.
post
(
"/dashboard/vm/1/"
,
{
'new_tag'
:
'test2'
})
self
.
assertEqual
(
response
.
status_code
,
302
)
self
.
assertEqual
(
inst
.
tags
.
count
(),
tag_count
+
1
)
def
test_permitted_add_tag_w_too_long_or_empty_tag
(
self
):
c
=
Client
()
self
.
login
(
c
,
"user2"
)
inst
=
Instance
.
objects
.
get
(
pk
=
1
)
inst
.
set_level
(
self
.
u2
,
'owner'
)
tag_count
=
inst
.
tags
.
count
()
response
=
c
.
post
(
"/dashboard/vm/1/"
,
{
'new_tag'
:
't'
*
30
})
self
.
assertEqual
(
response
.
status_code
,
302
)
response
=
c
.
post
(
"/dashboard/vm/1/"
,
{
'new_tag'
:
''
})
self
.
assertEqual
(
response
.
status_code
,
302
)
self
.
assertEqual
(
inst
.
tags
.
count
(),
tag_count
)
def
test_unpermitted_remove_tag
(
self
):
c
=
Client
()
self
.
login
(
c
,
"user2"
)
inst
=
Instance
.
objects
.
get
(
pk
=
1
)
inst
.
set_level
(
self
.
u2
,
'user'
)
tag_count
=
inst
.
tags
.
count
()
response
=
c
.
post
(
"/dashboard/vm/1/"
,
{
'to_remove'
:
'test1'
})
self
.
assertEqual
(
response
.
status_code
,
302
)
self
.
assertEqual
(
inst
.
tags
.
count
(),
tag_count
)
def
test_permitted_remove_tag
(
self
):
c
=
Client
()
self
.
login
(
c
,
"user2"
)
inst
=
Instance
.
objects
.
get
(
pk
=
1
)
inst
.
set_level
(
self
.
u2
,
'owner'
)
response
=
c
.
post
(
"/dashboard/vm/1/"
,
{
'new_tag'
:
'test1'
})
tag_count
=
inst
.
tags
.
count
()
response
=
c
.
post
(
"/dashboard/vm/1/"
,
{
'to_remove'
:
'test1'
})
self
.
assertEqual
(
response
.
status_code
,
302
)
self
.
assertEqual
(
inst
.
tags
.
count
(),
tag_count
-
1
)
def
test_permitted_remove_tag_w_ajax
(
self
):
c
=
Client
()
self
.
login
(
c
,
"user2"
)
inst
=
Instance
.
objects
.
get
(
pk
=
1
)
inst
.
set_level
(
self
.
u2
,
'owner'
)
response
=
c
.
post
(
"/dashboard/vm/1/"
,
{
'new_tag'
:
'test1'
})
tag_count
=
inst
.
tags
.
count
()
response
=
c
.
post
(
"/dashboard/vm/1/"
,
{
'to_remove'
:
'test1'
},
HTTP_X_REQUESTED_WITH
=
'XMLHttpRequest'
)
self
.
assertEqual
(
response
.
status_code
,
200
)
self
.
assertEqual
(
inst
.
tags
.
count
(),
tag_count
-
1
)
def
test_unpermitted_set_name
(
self
):
c
=
Client
()
self
.
login
(
c
,
"user2"
)
inst
=
Instance
.
objects
.
get
(
pk
=
1
)
inst
.
set_level
(
self
.
u2
,
'user'
)
old_name
=
inst
.
name
response
=
c
.
post
(
"/dashboard/vm/1/"
,
{
'new_name'
:
'test1235'
})
self
.
assertEqual
(
response
.
status_code
,
403
)
self
.
assertEqual
(
Instance
.
objects
.
get
(
pk
=
1
)
.
name
,
old_name
)
def
test_permitted_set_name
(
self
):
c
=
Client
()
self
.
login
(
c
,
"user2"
)
inst
=
Instance
.
objects
.
get
(
pk
=
1
)
inst
.
set_level
(
self
.
u2
,
'owner'
)
response
=
c
.
post
(
"/dashboard/vm/1/"
,
{
'new_name'
:
'test1234'
})
self
.
assertEqual
(
response
.
status_code
,
302
)
self
.
assertEqual
(
Instance
.
objects
.
get
(
pk
=
1
)
.
name
,
'test1234'
)
def
test_permitted_set_name_w_ajax
(
self
):
c
=
Client
()
self
.
login
(
c
,
"user2"
)
inst
=
Instance
.
objects
.
get
(
pk
=
1
)
inst
.
set_level
(
self
.
u2
,
'owner'
)
response
=
c
.
post
(
"/dashboard/vm/1/"
,
{
'new_name'
:
'test123'
},
HTTP_X_REQUESTED_WITH
=
'XMLHttpRequest'
)
self
.
assertEqual
(
response
.
status_code
,
200
)
self
.
assertEqual
(
Instance
.
objects
.
get
(
pk
=
1
)
.
name
,
'test123'
)
class
VmDetailVncTest
(
LoginMixin
,
TestCase
):
fixtures
=
[
'test-vm-fixture.json'
,
'node.json'
]
...
...
circle/dashboard/views.py
View file @
01eb957e
...
...
@@ -345,6 +345,9 @@ class VmDetailView(CheckedDetailView):
json
.
dumps
({
'message'
:
message
}),
content_type
=
"application=json"
)
else
:
return
redirect
(
reverse_lazy
(
"dashboard.views.detail"
,
kwargs
=
{
'pk'
:
self
.
object
.
pk
}))
def
__add_port
(
self
,
request
):
object
=
self
.
get_object
()
...
...
@@ -357,12 +360,20 @@ class VmDetailView(CheckedDetailView):
try
:
error
=
None
host
=
Host
.
objects
.
get
(
pk
=
request
.
POST
.
get
(
"host_pk"
))
interfaces
=
object
.
interface_set
.
all
()
host
=
Host
.
objects
.
get
(
pk
=
request
.
POST
.
get
(
"host_pk"
),
interface__in
=
interfaces
)
host
.
add_port
(
proto
,
private
=
port
)
except
Host
.
DoesNotExist
:
logger
.
error
(
'Tried to add port to nonexistent host
%
d. User:
%
s. '
'Instance:
%
s'
,
request
.
POST
.
get
(
"host_pk"
),
unicode
(
request
.
user
),
object
)
raise
PermissionDenied
()
except
ValueError
:
error
=
_
(
"There is a problem with your input!"
)
except
Exception
,
e
:
error
=
u', '
.
join
(
e
.
messages
)
except
Exception
as
e
:
error
=
_
(
"Unknown error."
)
logger
.
error
(
e
)
if
request
.
is_ajax
():
pass
...
...
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