Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
SRCT
go
Commits
bb7b3254
Commit
bb7b3254
authored
Nov 11, 2019
by
Zac Wood
Browse files
Fix edit behavior and add unit tests (!)
parent
8b0b9278
Pipeline
#5137
passed with stages
in 2 minutes and 1 second
Changes
4
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
go/go/forms.py
View file @
bb7b3254
...
...
@@ -117,6 +117,7 @@ class URLForm(ModelForm):
self
.
target_title
=
'Paste the URL you would like to shorten:'
self
.
short_title
=
'Create a custom Go address:'
self
.
expires_title
=
'Set when you would like your Go address to expire:'
self
.
action
=
'/newLink'
class
Meta
:
"""
...
...
@@ -135,3 +136,5 @@ class EditForm(URLForm):
self
.
target_title
=
'Modify the URL you would like to shorten:'
self
.
short_title
=
'Modify the Go address:'
self
.
expires_title
=
'Modify the expiration date:'
if
'initial'
in
kwargs
:
self
.
action
=
'/edit/'
+
kwargs
[
'initial'
][
'short'
]
\ No newline at end of file
go/go/templates/link.html
View file @
bb7b3254
...
...
@@ -27,7 +27,7 @@ SRCT Go • New Link
</div>
</div>
<form
class=
"form-horizontal"
action=
"
/newLink
"
method=
"post"
>
<form
class=
"form-horizontal"
action=
"
{{ form.action }}
"
method=
"post"
>
{% csrf_token %}
{{ form.non_field_errors }}
...
...
go/go/test_views.py
View file @
bb7b3254
...
...
@@ -112,10 +112,11 @@ class EditTest(TestCase):
"""
# Setup a blank URL object with an owner
User
.
objects
.
create
(
username
=
'dhaynes'
,
password
=
'password'
)
get_user
=
User
.
objects
.
get
(
username
=
'dhaynes'
)
get_registered_user
=
RegisteredUser
.
objects
.
get
(
user
=
get_user
)
URL
.
objects
.
create
(
owner
=
get_registered_user
,
short
=
'test'
)
self
.
user
=
User
.
objects
.
create
(
username
=
'dhaynes'
)
self
.
user
.
set_password
(
'test'
)
self
.
user
.
save
()
ru
=
RegisteredUser
.
objects
.
get
(
user
=
self
.
user
)
URL
.
objects
.
create
(
owner
=
ru
,
short
=
'test'
,
target
=
'https://google.com'
)
def
test_edit_get_anon
(
self
):
"""
...
...
@@ -126,6 +127,28 @@ class EditTest(TestCase):
response
=
self
.
client
.
get
(
'/edit/test'
)
self
.
assertEqual
(
response
.
status_code
,
302
)
def
test_edit_get_authed
(
self
):
c
=
Client
()
self
.
assertTrue
(
c
.
login
(
username
=
'dhaynes'
,
password
=
'test'
))
response
=
c
.
get
(
'/edit/test'
)
self
.
assertEqual
(
response
.
status_code
,
200
)
def
test_deletes_old_link
(
self
):
c
=
Client
()
self
.
assertTrue
(
c
.
login
(
username
=
'dhaynes'
,
password
=
'test'
))
c
.
post
(
'/edit/test'
,
{
'short'
:
'newtest'
,
'target'
:
'https://google.com'
,
'expires'
:
'Never'
})
self
.
assertEqual
(
0
,
URL
.
objects
.
filter
(
short
=
'test'
).
count
())
self
.
assertEqual
(
1
,
URL
.
objects
.
filter
(
short
=
'newtest'
).
count
())
def
test_wrong_user
(
self
):
u
=
User
.
objects
.
create
(
username
=
'zwood2'
)
u
.
set_password
(
'test'
)
u
.
save
()
c
=
Client
()
self
.
assertTrue
(
c
.
login
(
username
=
'zwood2'
,
password
=
'test'
))
response
=
c
.
get
(
'/edit/test'
)
self
.
assertEqual
(
403
,
response
.
status_code
)
class
DeleteTest
(
TestCase
):
"""
...
...
@@ -138,11 +161,11 @@ class DeleteTest(TestCase):
testing methods
"""
# Setup a blank URL object with an owner
U
se
r
.
objects
.
create
(
username
=
'dhaynes'
,
password
=
'password
'
)
get_user
=
User
.
objects
.
get
(
username
=
'dhaynes'
)
get_registered_use
r
=
RegisteredUser
.
objects
.
get
(
user
=
get_
user
)
URL
.
objects
.
create
(
owner
=
get_registered_user
,
short
=
'test
'
)
self
.
user
=
User
.
objects
.
create
(
username
=
'dhaynes'
)
se
lf
.
user
.
set_password
(
'test
'
)
self
.
user
.
save
(
)
r
u
=
RegisteredUser
.
objects
.
get
(
user
=
self
.
user
)
URL
.
objects
.
create
(
owner
=
ru
,
short
=
'test'
,
target
=
'https://google.com
'
)
def
test_delete_get_anon
(
self
):
"""
...
...
@@ -153,6 +176,23 @@ class DeleteTest(TestCase):
response
=
self
.
client
.
get
(
'/delete/test'
)
self
.
assertEqual
(
response
.
status_code
,
302
)
def
test_deletes_link
(
self
):
c
=
Client
()
self
.
assertTrue
(
c
.
login
(
username
=
'dhaynes'
,
password
=
'test'
))
self
.
assertEqual
(
1
,
URL
.
objects
.
filter
(
short
=
'test'
).
count
())
c
.
get
(
'/delete/test'
)
self
.
assertEqual
(
0
,
URL
.
objects
.
filter
(
short
=
'test'
).
count
())
def
test_wrong_user
(
self
):
u
=
User
.
objects
.
create
(
username
=
'zwood2'
)
u
.
set_password
(
'test'
)
u
.
save
()
c
=
Client
()
self
.
assertTrue
(
c
.
login
(
username
=
'zwood2'
,
password
=
'test'
))
response
=
c
.
get
(
'/delete/test'
)
self
.
assertEqual
(
403
,
response
.
status_code
)
class
RedirectionTest
(
TestCase
):
"""
...
...
go/go/views.py
View file @
bb7b3254
...
...
@@ -195,105 +195,107 @@ def edit(request, short):
url
=
get_object_or_404
(
URL
,
short__iexact
=
short
)
# If the RegisteredUser is the owner of the URL
if
url
.
owner
!=
request
.
user
.
registereduser
:
if
url
.
owner
==
request
.
user
.
registereduser
:
# If a POST request is received, then the user has submitted a form and it's
# time to parse the form and edit that URL object
if
request
.
method
==
'POST'
:
# Now we initialize the form again but this time we have the POST
# request
url_form
=
EditForm
(
request
.
POST
,
host
=
request
.
META
.
get
(
'HTTP_HOST'
))
# Make a copy of the old URL
copy
=
url
# Remove the old one
url
.
delete
()
# Django will check the form to make sure it's valid
if
url_form
.
is_valid
():
# If the short changed then we need to create a new object and
# migrate some data over
if
url_form
.
cleaned_data
.
get
(
'short'
).
strip
()
!=
copy
.
short
:
# Parse the form and create a new URL object
res
=
post
(
request
,
url_form
)
# If there is a 500 error returned, handle it
if
res
==
500
:
return
HttpResponseServerError
(
render
(
request
,
'500.html'
))
# We can procede with the editing process
else
:
# Migrate clicks data
res
.
clicks
=
copy
.
clicks
res
.
qrclicks
=
copy
.
qrclicks
res
.
socialclicks
=
copy
.
socialclicks
# Save the new URL
res
.
save
()
# Redirect to the shiny new *edited URL
return
redirect
(
'view'
,
res
.
short
)
# The short was not edited and thus, we can directly edit the url
else
:
if
url_form
.
cleaned_data
.
get
(
'target'
).
strip
()
!=
copy
.
target
:
copy
.
target
=
url_form
.
cleaned_data
.
get
(
'target'
).
strip
()
copy
.
save
()
# Grab the expiration field value. It's currently an unsable
# string value, so we need to parse it into a datetime object
# relative to right now.
expires
=
url_form
.
cleaned_data
.
get
(
'expires'
)
# Determine what the expiration date is
if
expires
==
URLForm
.
DAY
:
edited_expires
=
timezone
.
now
()
+
timedelta
(
days
=
1
)
elif
expires
==
URLForm
.
WEEK
:
edited_expires
=
timezone
.
now
()
+
timedelta
(
weeks
=
1
)
elif
expires
==
URLForm
.
MONTH
:
edited_expires
=
timezone
.
now
()
+
timedelta
(
weeks
=
3
)
elif
expires
==
URLForm
.
CUSTOM
:
edited_expires
=
url_form
.
cleaned_data
.
get
(
'expires_custom'
)
else
:
pass
# leave the field NULL
if
edited_expires
!=
copy
.
expires
:
copy
.
expires
=
edited_expires
copy
.
save
()
# Redirect to the shiny new *edited URL
return
redirect
(
'view'
,
copy
.
short
)
# Else, there is an error, redisplay the form with the validation errors
else
:
# Render index.html passing the form to the template
return
render
(
request
,
'link.html'
,
{
'form'
:
url_form
})
else
:
# Initial data set here
if
url
.
expires
!=
None
:
# Initialize a URL form with an expire date
url_form
=
EditForm
(
host
=
request
.
META
.
get
(
'HTTP_HOST'
),
initial
=
{
'target'
:
url
.
target
,
'short'
:
url
.
short
,
'expires'
:
'Custom Date'
,
'expires_custom'
:
url
.
expires
})
# unbound form
else
:
# Initialize a URL form without an expire date
url_form
=
EditForm
(
host
=
request
.
META
.
get
(
'HTTP_HOST'
),
initial
=
{
'target'
:
url
.
target
,
'short'
:
url
.
short
,
'expires'
:
'Never'
,
})
# unbound form
# Render index.html passing the form to the template
return
render
(
request
,
'link.html'
,
{
'form'
:
url_form
})
else
:
# do not allow them to edit
raise
PermissionDenied
()
# If a POST request is received, then the user has submitted a form and it's
# time to parse the form and edit that URL object
if
request
.
method
==
'POST'
:
return
_edit_post
(
request
,
url
)
# Initial data set here
if
url
.
expires
!=
None
:
# Initialize a URL form with an expire date
url_form
=
EditForm
(
host
=
request
.
META
.
get
(
'HTTP_HOST'
),
initial
=
{
'target'
:
url
.
target
,
'short'
:
url
.
short
,
'expires'
:
'Custom Date'
,
'expires_custom'
:
url
.
expires
})
# unbound form
else
:
# Initialize a URL form without an expire date
url_form
=
EditForm
(
host
=
request
.
META
.
get
(
'HTTP_HOST'
),
initial
=
{
'target'
:
url
.
target
,
'short'
:
url
.
short
,
'expires'
:
'Never'
,
})
# unbound form
# Render index.html passing the form to the template
return
render
(
request
,
'link.html'
,
{
'form'
:
url_form
})
def
_edit_post
(
request
,
url
):
# Now we initialize the form again but this time we have the POST
# request
url_form
=
EditForm
(
request
.
POST
,
host
=
request
.
META
.
get
(
'HTTP_HOST'
))
# Make a copy of the old URL
copy
=
url
# Remove the old one
url
.
delete
()
# Django will check the form to make sure it's valid
if
not
url_form
.
is_valid
():
# Render index.html passing the form to the template
return
render
(
request
,
'link.html'
,
{
'form'
:
url_form
})
# If the short changed then we need to create a new object and
# migrate some data over
if
url_form
.
cleaned_data
.
get
(
'short'
).
strip
()
!=
copy
.
short
:
# Parse the form and create a new URL object
res
=
post
(
request
,
url_form
)
# If there is a 500 error returned, handle it
if
res
==
500
:
return
HttpResponseServerError
(
render
(
request
,
'500.html'
))
# Else we can procede with the editing process
# Migrate clicks data
res
.
clicks
=
copy
.
clicks
res
.
qrclicks
=
copy
.
qrclicks
res
.
socialclicks
=
copy
.
socialclicks
# Save the new URL
res
.
save
()
# Redirect to the shiny new *edited URL
return
redirect
(
'view'
,
res
.
short
)
# The short was not edited and thus, we can directly edit the url
if
url_form
.
cleaned_data
.
get
(
'target'
).
strip
()
!=
copy
.
target
:
copy
.
target
=
url_form
.
cleaned_data
.
get
(
'target'
).
strip
()
copy
.
save
()
# Grab the expiration field value. It's currently an unsable
# string value, so we need to parse it into a datetime object
# relative to right now.
expires
=
url_form
.
cleaned_data
.
get
(
'expires'
)
# Determine what the expiration date is
if
expires
==
URLForm
.
DAY
:
edited_expires
=
timezone
.
now
()
+
timedelta
(
days
=
1
)
elif
expires
==
URLForm
.
WEEK
:
edited_expires
=
timezone
.
now
()
+
timedelta
(
weeks
=
1
)
elif
expires
==
URLForm
.
MONTH
:
edited_expires
=
timezone
.
now
()
+
timedelta
(
weeks
=
3
)
elif
expires
==
URLForm
.
CUSTOM
:
edited_expires
=
url_form
.
cleaned_data
.
get
(
'expires_custom'
)
else
:
pass
# leave the field NULL
if
edited_expires
!=
copy
.
expires
:
copy
.
expires
=
edited_expires
copy
.
save
()
# Redirect to the shiny new *edited URL
return
redirect
(
'view'
,
copy
.
short
)
@
login_required
def
delete
(
request
,
short
):
"""
...
...
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