Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
KrasnovAS
short_url_generator
Commits
31c8de80
Commit
31c8de80
authored
Oct 08, 2020
by
AntonKras
Browse files
тесты и исправления
parent
ed781042
Changes
4
Hide whitespace changes
Inline
Side-by-side
app.py
View file @
31c8de80
from
flask
import
Flask
,
render_template
,
redirect
,
request
,
abort
,
flash
import
re
,
os
,
random
,
string
,
yaml
,
redis
,
sys
import
argparse
app
=
Flask
(
__name__
)
app
.
secret_key
=
os
.
urandom
(
24
)
@
app
.
route
(
'/'
,
methods
=
[
'GET'
,
'POST'
])
def
main
():
"""
функция рендерит шаблон
:return отрендеренный html-файл:
"""
yaml_dict
=
extract_data_from_yml
(
file
)
yaml_dict
=
extract_data_from_yml
(
sys
.
argv
[
1
]
)
return
render_template
(
'index.html'
,
yaml_dict
=
yaml_dict
)
...
...
@@ -22,8 +20,8 @@ def add():
функция добавления соответствия
:return redirect(перенаправление на главную страницу):
"""
url
=
request
.
args
.
get
(
"url
1
"
)
yaml_dict
=
extract_data_from_yml
(
file
)
url
=
request
.
args
.
get
(
"url"
)
yaml_dict
=
extract_data_from_yml
(
sys
.
argv
[
1
]
)
if
check_if_input_is_url
(
url
)
is
None
:
flash
(
'URL введен некорректно!'
)
return
redirect
(
'http://127.0.0.1:5000/'
)
...
...
@@ -33,7 +31,7 @@ def add():
else
:
short_url
=
generate_short_url
()
data
=
{
short_url
:
url
}
with
open
(
file
,
'a+'
,
encoding
=
'utf8'
)
as
f
:
with
open
(
sys
.
argv
[
1
]
,
'a+'
,
encoding
=
'utf8'
)
as
f
:
yaml
.
dump
(
data
,
f
,
default_flow_style
=
False
,
allow_unicode
=
True
)
f
.
close
()
return
redirect
(
'http://127.0.0.1:5000/'
)
...
...
@@ -46,7 +44,7 @@ def redirect_somewhere(path):
:param path (передается часть ссылки после /):
:return redirect or 404 Error:
"""
yml_dict
=
extract_data_from_yml
(
file
)
yml_dict
=
extract_data_from_yml
(
sys
.
argv
[
1
]
)
if
path
in
yml_dict
:
return
redirect
(
normalize
(
yml_dict
[
path
]))
else
:
...
...
@@ -112,5 +110,4 @@ def normalize(url: str) -> str:
if
__name__
==
'__main__'
:
file
=
sys
.
argv
[
1
]
app
.
run
()
static/urls.yml
View file @
31c8de80
...
...
@@ -7,3 +7,4 @@ mwimu: https://radiojazzfm.ru/
ahfsm
:
https://ru.wikipedia.org/wiki/%D0%9F%D0%BE%D1%82%D0%B5%D1%80%D1%8F%D0%BD%D0%BD%D0%BE%D0%B5_%D0%BF%D0%BE%D0%BA%D0%BE%D0%BB%D0%B5%D0%BD%D0%B8%D0%B5
ulido
:
https://pythonbasics.org/flask-tutorial-routes/
xcurl
:
https://www.imdb.com/list/ls025873906/?ref_=otl_3
nbnvo
:
https://ru.wikipedia.org/wiki/%D0%A1%D0%BF%D0%B8%D1%81%D0%BE%D0%BA_%D1%83%D0%BB%D0%B8%D1%86_%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D1%8B
templates/index.html
View file @
31c8de80
...
...
@@ -7,7 +7,7 @@
<body>
<p><b>
Введите длинную ссылку:
</b><br>
<form
action=
"/add"
>
<input
type=
"text"
size=
"40"
name=
"url
1
"
>
<input
type=
"text"
size=
"40"
name=
"url"
>
<button>
Создать новое соответствие
</button>
{% with messages = get_flashed_messages() %}
{% if messages %}
...
...
tests.py
View file @
31c8de80
from
flask_testing
import
TestCase
from
flask
import
Flask
from
flask
import
url_for
,
request
,
redirect
import
urllib.request
as
urllib2
from
flask_testing
import
LiveServerTestCase
from
app
import
app
import
unittest
import
sys
import
flask_testing
import
click
from
unittest.mock
import
patch
class
TestPageLoad
(
unittest
.
TestCase
):
def
setUp
(
self
):
"""
создание тестового веб-сервиса
"""
app
.
config
[
'TESTING'
]
=
True
app
.
config
[
'WTF_CSRF_ENABLED'
]
=
False
app
.
config
[
'DEBUG'
]
=
False
sys
.
argv
=
[
'app.py'
,
'static/urls.yml'
]
self
.
app
=
app
.
test_client
()
self
.
assertEqual
(
app
.
debug
,
False
)
def
test_main_page
(
self
):
# sys.argv = r'C:\Users\Dell\PycharmProjects\short_url_generator\static\urls.yml'
def
test_main_page
(
self
):
response
=
self
.
app
.
get
(
'/'
,
follow_redirects
=
True
)
self
.
assertEqual
(
response
.
status_code
,
200
)
def
test_wrong_short_url
(
self
):
tester
=
app
.
test_client
(
self
)
response
=
tester
.
get
(
'/wwww'
,
content_type
=
'html/text'
)
self
.
assertEqual
(
response
.
status_code
,
404
)
if
__name__
==
'__main__'
:
def
test_right_short_url
(
self
):
tester
=
app
.
test_client
(
self
)
response
=
tester
.
get
(
'/yah44'
,
content_type
=
'html/text'
)
self
.
assertEqual
(
response
.
status_code
,
302
)
# def test_redirect(self):
# tester = app.test_client(self)
# with tester:
# response = tester.get(url_for('/yah44'), follow_redirects=True)
# assert request.path == url_for('https://www.yahoo.com/')
if
__name__
==
'__main__'
:
unittest
.
main
()
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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