Skip to content
This repository was archived by the owner on Aug 4, 2018. It is now read-only.

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Fingercomp committed Feb 22, 2017
2 parents bbb715f + f9f619d commit 645678a
Show file tree
Hide file tree
Showing 8 changed files with 265 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
3.3.0
-----
- Added ``versions.files.path``, deprected ``versions.files.dir`` and ``versions.files.name``.

3.2.0
-----
- Made it possible to change user password.
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2016 Dmitry "Totoro" Zhidenkov, Alexander "Fingercomp" Yanin
Copyright (c) 2016-2017 Dmitry "Totoro" Zhidenkov, Alexander "Fingercomp"

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
151 changes: 149 additions & 2 deletions hel/tests/test_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,97 @@ def test_upd_pkg_ver_v_files_name_str(self):
self.assertEqual(Messages.type_mismatch % ('file_name', 'str',),
res.json['message'])

def test_upd_pkg_ver_v_files_name_bad(self):
res = self.test_app.patch_json('/packages/package-1', {
'versions': {
'1.1.1': {
'files': {
'http://example.com/file18': {
'name': 'hey/there'
}
}
}
}
}, headers=self.auth_headers, status=400)
self.assertEqual(Messages.bad_path, res.json['message'])

def test_upd_pkg_ver_v_files_path_bad(self):
res = self.test_app.patch_json('/packages/package-1', {
'versions': {
'1.1.1': {
'files': {
'http://example.com/file18': {
'path': '/hey/there/'
}
}
}
}
}, headers=self.auth_headers, status=400)
self.assertEqual(Messages.bad_path, res.json['message'])

def test_upd_pkg_ver_v_files_path(self):
self.test_app.patch_json('/packages/package-1', {
'versions': {
'1.1.1': {
'files': {
'http://example.com/file18': {
'path': 'hey/there'
}
}
}
}
}, headers=self.auth_headers, status=204)
res = self.test_app.get('/packages/package-1', status=200)
self.assertEqual((res.json['data']['versions']['1.1.1']['files']
['http://example.com/file18']['path']), '/hey/there')
self.assertEqual((res.json['data']['versions']['1.1.1']['files']
['http://example.com/file18']['dir']), '/hey')
self.assertEqual((res.json['data']['versions']['1.1.1']['files']
['http://example.com/file18']['name']), 'there')

def test_upd_pkg_ver_v_files_name_to_path(self):
self.test_app.patch_json('/packages/package-1', {
'versions': {
'1.1.1': {
'files': {
'http://example.com/file18': {
'name': 'new-name'
}
}
}
}
}, headers=self.auth_headers, status=204)
res = self.test_app.get('/packages/package-1', status=200)
self.assertEqual((res.json['data']['versions']['1.1.1']['files']
['http://example.com/file18']['path']),
'/lib/new-name')
self.assertEqual((res.json['data']['versions']['1.1.1']['files']
['http://example.com/file18']['dir']), '/lib')
self.assertEqual((res.json['data']['versions']['1.1.1']['files']
['http://example.com/file18']['name']), 'new-name')

def test_upd_pkg_ver_v_files_dir_to_path(self):
self.test_app.patch_json('/packages/package-1', {
'versions': {
'1.1.1': {
'files': {
'http://example.com/file18': {
'dir': 'opt'
}
}
}
}
}, headers=self.auth_headers, status=204)
res = self.test_app.get('/packages/package-1', status=200)
self.assertEqual((res.json['data']['versions']['1.1.1']['files']
['http://example.com/file18']['path']),
'/opt/test-1-file-8')
self.assertEqual((res.json['data']['versions']['1.1.1']['files']
['http://example.com/file18']['dir']), '/opt')
self.assertEqual((res.json['data']['versions']['1.1.1']['files']
['http://example.com/file18']['name']),
'test-1-file-8')

def test_upd_pkg_ver_v_deps_dict(self):
res = self.test_app.patch_json('/packages/package-2', {
'versions': {
Expand Down Expand Up @@ -1104,21 +1195,77 @@ def test_crt_pkg_owners_empty(self):
self.assertEqual(Messages.empty_owner_list, res.json['message'])

def test_crt_pkg_name_conflict(self):
pkg = copy.copy(self.pkg3.data)
pkg = copy.deepcopy(self.pkg3.data)
pkg['name'] = 'package-1'
res = self.test_app.post_json(
'/packages', pkg,
headers=self.auth_headers, status=409)
self.assertEqual(Messages.pkg_name_conflict, res.json['message'])

def test_crt_pkg_name_bad(self):
pkg = copy.copy(self.pkg3.data)
pkg = copy.deepcopy(self.pkg3.data)
pkg['name'] = 'hi.there'
res = self.test_app.post_json(
'/packages', pkg,
headers=self.auth_headers, status=400)
self.assertEqual(Messages.pkg_bad_name, res.json['message'])

def test_crt_pkg_bad_filename(self):
pkg = copy.deepcopy(self.pkg3.data)
pkg['versions']['1.1.0']['files']['http://example.com/file34'] = {
'name': 'test/test-3-file-4',
'dir': '/bin'
}
res = self.test_app.post_json(
'/packages', pkg,
headers=self.auth_headers, status=400)
self.assertEqual(Messages.bad_path, res.json['message'])

def test_crt_pkg_no_path(self):
pkg = copy.deepcopy(self.pkg3.data)
pkg['versions']['1.1.0']['files']['http://example.com/file34'] = {}
self.test_app.post_json(
'/packages', pkg,
headers=self.auth_headers, status=400)

def test_crt_pkg_bad_path(self):
pkg = copy.deepcopy(self.pkg3.data)
pkg['versions']['1.1.0']['files']['http://example.com/file34'] = {
'path': '/hey/there/path/'
}
res = self.test_app.post_json(
'/packages', pkg,
headers=self.auth_headers, status=400)
self.assertEqual(Messages.bad_path, res.json['message'])

def test_crt_pkg_path(self):
pkg = copy.deepcopy(self.pkg3.data)
pkg['versions']['1.1.0']['files']['http://example.com/file34'] = {
'path': 'bin/test-3-file-4'
}
self.test_app.post_json(
'/packages', pkg,
headers=self.auth_headers, status=201)
res = self.test_app.get('/packages/package-3', status=200)
self.assertEqual((res.json['data']['versions']['1.1.0']['files']
['http://example.com/file34']['path']),
'/bin/test-3-file-4')
self.assertEqual((res.json['data']['versions']['1.1.0']['files']
['http://example.com/file34']['dir']),
'/bin')
self.assertEqual((res.json['data']['versions']['1.1.0']['files']
['http://example.com/file34']['name']),
'test-3-file-4')

def test_crt_pkg_convert_path(self):
self.test_app.post_json(
'/packages', self.pkg3.data,
headers=self.auth_headers, status=201)
res = self.test_app.get('/packages/package-3', status=200)
self.assertEqual((res.json['data']['versions']['1.1.0']['files']
['http://example.com/file34']['path']),
'/bin/test-3-file-4')

def test_lst_pkgs_no_params(self):
res = self.test_app.get('/packages', {
'name': 'age pack'
Expand Down
2 changes: 1 addition & 1 deletion hel/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json


VERSION = '3.2.0'
VERSION = '3.3.0'


def parse_search_phrase(s):
Expand Down
1 change: 1 addition & 0 deletions hel/utils/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ class Messages:
type_mismatch = 'Wrong value for param "%s" given: expected %s!'
user_bad_name = 'The nickname contains illegal characters.'
wrong_dep_type = 'Unknown dependency type.'
bad_path = 'Bad path given.'
31 changes: 28 additions & 3 deletions hel/utils/models.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
# -*- coding: utf-8 -*-
import datetime
import json
import os.path

from pyramid.httpexceptions import HTTPBadRequest
import semantic_version as semver

from hel.utils.constants import Constants
from hel.utils.messages import Messages
from hel.utils.query import replace_chars_in_keys, parse_url
from hel.utils.query import (
replace_chars_in_keys,
parse_url,
split_path,
check_path,
check_filename
)


class ModelPackage:
Expand Down Expand Up @@ -59,9 +66,27 @@ def __init__(self, strict=False, **kwargs):
files = {}
for file_url_unchecked, f in value['files'].items():
file_url = parse_url(file_url_unchecked)
file_dir = file_name = file_path = None
if ('path' not in f and
'dir' not in f and
'name' not in f):
f['path'] # raise an error
if 'path' in f:
check_path(str(f['path']))
file_path = os.path.join('/', str(f['path']))
file_dir, file_name = split_path(file_path)
else:
check_path(str(f['name']))
check_filename(str(f['name']))
file_dir, file_name = str(f['dir']), str(f['name'])
file_path = os.path.join('/', f['dir'], f['name'])
file_dir = os.path.normpath(file_dir)
file_name = os.path.normpath(file_name)
file_path = os.path.normpath(file_path)
files[str(file_url)] = {
'dir': str(f['dir']),
'name': str(f['name'])
'dir': file_dir,
'name': file_name,
'path': file_path
}
dependencies = {}
for dep_name, dep_info in value['depends'].items():
Expand Down
18 changes: 18 additions & 0 deletions hel/utils/query.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
import os.path

from pyramid.httpexceptions import HTTPBadRequest
import rfc3987

Expand Down Expand Up @@ -377,3 +379,19 @@ def parse_url(url):
matches['path'] = matches['path'] or '/'
matches['fragment'] = None
return rfc3987.compose(**matches)


def check_path(path):
if path[-1] == '/':
raise HTTPBadRequest(detail=Messages.bad_path)
return True


def split_path(path):
return os.path.split(os.path.join('/', path))


def check_filename(name):
if name.find('/') != -1:
raise HTTPBadRequest(detail=Messages.bad_path)
return True
80 changes: 63 additions & 17 deletions hel/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import hashlib
import logging
import os
import os.path

from pyramid.httpexceptions import (
HTTPBadRequest,
Expand Down Expand Up @@ -32,7 +33,10 @@
check,
check_list_of_strs,
parse_url,
replace_chars_in_keys
replace_chars_in_keys,
check_path,
split_path,
check_filename
)


Expand Down Expand Up @@ -253,17 +257,48 @@ def update_package(context, request):
Messages.type_mismatch % ('file_info', 'dict',))
if ((num not in old['versions'] or
url not in old['versions'][num]['files']) and
('dir' not in file_info or
'name' not in file_info)):
(('dir' not in file_info or
'name' not in file_info) and
'path' not in file_info)):
raise HTTPBadRequest(detail=Messages.partial_ver)
if ('dir' in file_info and
check(file_info['dir'], str,
Messages.type_mismatch % (
'file_dir', 'str',)) or
'name' in file_info and
check(file_info['name'], str,
Messages.type_mismatch % (
'file_name', 'str',))):
if ('path' in file_info and
check(file_info['path'], str,
Messages.type_mismatch % ('file_path',
'str',)) and
check_path(file_info['path'])):
if k not in query:
query[k] = {}
if num not in query[k]:
query[k][num] = {}
if 'files' not in query[k][num]:
query[k][num]['files'] = {}
if url not in query[k][num]['files']:
query[k][num]['files'][url] = {}
(query[k][num]['files'][url]
['path']) = os.path.join('/', file_info['path'])
(query[k][num]['files'][url]['dir'],
query[k][num]['files'][url]['name']) = (
split_path(os.path.join('/',
file_info['path'])))
query[k][num]['files'][url]['path'] = (
os.path.normpath(query[k][num]['files'][url]
['path']))
query[k][num]['files'][url]['dir'] = (
os.path.normpath(query[k][num]['files'][url]
['dir']))
query[k][num]['files'][url]['name'] = (
os.path.normpath(query[k][num]['files'][url]
['name']))
elif ('dir' in file_info and
check(file_info['dir'], str,
Messages.type_mismatch % ('file_dir',
'str',)) or
'name' in file_info and
check(file_info['name'], str,
Messages.type_mismatch % ('file_name',
'str',)) and
check_path(file_info['name']) and
check_filename(file_info['name'])):
if k not in query:
query[k] = {}
if num not in query[k]:
Expand All @@ -273,12 +308,23 @@ def update_package(context, request):
if url not in query[k][num]['files']:
(query[k][num]['files']
[url]) = {}
if 'dir' in file_info:
(query[k][num]['files']
[url]['dir']) = file_info['dir']
if 'name' in file_info:
(query[k][num]['files']
[url]['name']) = file_info['name']
path = None
if 'dir' in file_info:
(query[k][num]['files']
[url]['dir']) = os.path.normpath(
os.path.join('/', file_info['dir']))
path = query[k][num]['files'][url]['dir']
else:
path = old[k][num]['files'][url]['dir']
if 'name' in file_info:
(query[k][num]['files']
[url]['name']) = file_info['name']
path = os.path.join(path, file_info['name'])
else:
path = os.path.normpath(os.path.join(
path,
old[k][num]['files'][url]['name']))
query[k][num]['files'][url]['path'] = path

if 'depends' in ver:
check(ver['depends'], dict,
Expand Down

0 comments on commit 645678a

Please sign in to comment.