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

Commit

Permalink
Merge pull request #17 from hel-repo/dev
Browse files Browse the repository at this point in the history
Hel 3.1.0
  • Loading branch information
Fingercomp authored Nov 24, 2016
2 parents 131f8f2 + 4b0ab07 commit 2ea5824
Show file tree
Hide file tree
Showing 8 changed files with 276 additions and 31 deletions.
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
3.1.0
-----
- Added ``owners`` search param.
- Added ``q`` search param.
- Added group ``~banned``.
- Wrapped ``User:PATCH``.

3.0.3
-----
- Added dependency remove code.
Expand Down
12 changes: 10 additions & 2 deletions hel/resources.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from bson.objectid import ObjectId
from pyramid.security import Allow, Everyone, Authenticated, ALL_PERMISSIONS
from pyramid.security import (
Allow,
Deny,
Everyone,
Authenticated,
ALL_PERMISSIONS
)
from pyramid.traversal import find_root


Expand All @@ -25,8 +31,10 @@ def __acl__(cls):
(Allow, '~system', ALL_PERMISSIONS,),
(Allow, Everyone, 'pkg_view',),
(Allow, Everyone, 'pkgs_view',),
(Deny, '~banned', 'pkg_create',),
(Allow, Authenticated, 'pkg_create',), # TODO: Activated only
(Allow, Everyone, 'user_list',)
(Allow, Everyone, 'user_list',),
(Deny, '~banned', ALL_PERMISSIONS,)
]


Expand Down
131 changes: 123 additions & 8 deletions hel/tests/test_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,24 +253,104 @@ def test_create_bad_pkg(self):
'blah': 'blah'
}, headers=self.auth_headers, status=400)

def test_get_user(self):
res = self.test_app.get('/users/root',
headers=self.auth_headers, status=200)
self.assertEqual(res.json['data']['nickname'], 'root')

def test_upd_user_404(self):
client = MongoClient(mongodb_url)
client.hel['users'].update(
{'nickname': 'root'},
{'$set': {'groups': ['admins']}})
client.close()
self.test_app.patch_json('/users/no-such-user', {},
headers=self.auth_headers, status=404)

def test_upd_user_malformed_json(self):
client = MongoClient(mongodb_url)
client.hel['users'].update(
{'nickname': 'root'},
{'$set': {'groups': ['admins']}})
client.close()
res = self.test_app.patch('/users/root', '''oh"ai''',
headers=self.auth_headers, status=400)
self.assertEqual(res.json['message'], Messages.bad_request)

def test_upd_user(self):
client = MongoClient(mongodb_url)
client.hel['users'].update(
{'nickname': 'root'},
{'$set': {'groups': ['admins']}})
client.close()
self.test_app.patch_json('/users/root', {
'$set': {
'groups': [
'hi'
]
}
'nickname': 'square-root',
'groups': ['hi']
}, headers=self.auth_headers, status=204)
res = self.test_app.get(
'/users/root',
'/users',
headers=self.auth_headers, status=200)
data = res.json['data']
self.assertIn('hi', data['groups'])
data = res.json['data']['list']
user_dict = {}
for v in data:
user_dict[v['nickname']] = v
self.assertIn('square-root', user_dict)
self.assertIn('hi', user_dict['square-root']['groups'])
self.log_out_status = 400

def test_upd_user_bad_name(self):
client = MongoClient(mongodb_url)
client.hel['users'].update(
{'nickname': 'root'},
{'$set': {'groups': ['admins']}})
client.close()
res = self.test_app.patch_json('/users/root', {
'nickname': '-n87`\\a'
}, headers=self.auth_headers, status=400)
self.assertEqual(res.json["message"], Messages.user_bad_name)

def test_upd_user_name_conflict(self):
client = MongoClient(mongodb_url)
client.hel['users'].update(
{'nickname': 'root'},
{'$set': {'groups': ['admins']}})
client.hel['users'].insert_one({'nickname': 'square-root',
'email': '[email protected]',
'password': 'squareit!',
'activation_phrase': 'test',
'activation_till': 'test',
'groups': []})
client.close()
self.test_app.get('/users/square-root',
headers=self.auth_headers, status=200)
res = self.test_app.patch_json('/users/root', {
'nickname': 'square-root'
}, headers=self.auth_headers, status=400)
self.assertEqual(res.json["message"], Messages.nickname_in_use)

def test_upd_user_nick_type(self):
client = MongoClient(mongodb_url)
client.hel['users'].update(
{'nickname': 'root'},
{'$set': {'groups': ['admins']}})
client.close()
res = self.test_app.patch_json('/users/root', {
'nickname': ['test']
}, headers=self.auth_headers, status=400)
self.assertEqual(res.json["message"],
Messages.type_mismatch % ('nickname', 'str',))

def test_upd_user_group_type(self):
client = MongoClient(mongodb_url)
client.hel['users'].update(
{'nickname': 'root'},
{'$set': {'groups': ['admins']}})
client.close()
res = self.test_app.patch_json('/users/root', {
'groups': 'test'
}, headers=self.auth_headers, status=400)
self.assertEqual(res.json["message"],
Messages.type_mismatch % ('groups', 'list of strs',))

def test_get_non_existing_user(self):
client = MongoClient(mongodb_url)
Expand All @@ -293,6 +373,15 @@ def test_create_bad_user(self):
}, headers=self.auth_headers, status=400)
self.assertEqual(Messages.bad_user, res.json['message'])

def test_del_user_404(self):
client = MongoClient(mongodb_url)
client.hel['users'].update(
{'nickname': 'root'},
{'$set': {'groups': ['admins']}})
client.close()
self.test_app.delete('/users/no-such-user',
headers=self.auth_headers, status=404)

def test_del_user(self):
client = MongoClient(mongodb_url)
client.hel['users'].update(
Expand Down Expand Up @@ -462,6 +551,26 @@ def test_stats_views_increment(self):
res = self.test_app.get('/packages/package-2', status=200)
self.assertEqual(res.json['data']['stats']['views'], 3)

def test_upd_pkg_404(self):
client = MongoClient(mongodb_url)
client.hel['users'].update(
{'nickname': 'root'},
{'$set': {'groups': ['admins']}})
client.close()
self.test_app.patch_json('/packages/no-such-package', {
'name': 'hi'
}, headers=self.auth_headers, status=404)

def test_upd_pkg_malformed_json(self):
client = MongoClient(mongodb_url)
client.hel['users'].update(
{'nickname': 'root'},
{'$set': {'groups': ['admins']}})
client.close()
res = self.test_app.patch('/packages/package-1', '''oh"ai''',
headers=self.auth_headers, status=400)
self.assertEqual(res.json['message'], Messages.bad_request)

def test_upd_pkg_name_conflict(self):
res = self.test_app.patch_json('/packages/package-1', {
'name': 'package-1'
Expand Down Expand Up @@ -934,6 +1043,12 @@ def test_del_pkg(self):
self.test_app.delete(
'/packages/package-2',
headers=self.auth_headers, status=204)
self.test_app.get('/packages/package-2', status=404)

def test_crt_pkg_malformed_json(self):
res = self.test_app.post('/packages', '''oh"ai''',
headers=self.auth_headers, status=400)
self.assertEqual(res.json['message'], Messages.bad_request)

def test_crt_pkg_owners_bad_user(self):
pkg = copy.deepcopy(self.pkg3.data)
Expand Down
18 changes: 18 additions & 0 deletions hel/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,15 @@ def test_pkg_search_author(self):
('Kjers', [self.pkg2],)
]

@param('owners')
def test_pkg_search_owners(self):
return [
(['root'], [self.pkg1, self.pkg2, self.pkg3],),
(['test'], [],),
(['root', 'test'], [],),
(['root', 'crackes'], [self.pkg1],)
]

@one_value_param('screen_desc')
def test_pkg_search_screen_desc(self):
return [
Expand Down Expand Up @@ -251,6 +260,15 @@ def test_pkg_search_screen_url(self):
(['http://img.example.com/img32'], [self.pkg3],)
]

@one_value_param('q')
def test_pkg_search_q(self):
return [
('root', [self.pkg1, self.pkg2, self.pkg3],),
('Change package "My first"', [self.pkg1],),
('hello people xD', [],),
('test img-2 2 21.', [self.pkg2],)
]

def test_bad_search_param(self):
try:
PackagesSearcher({'hi': ['test']})()
Expand Down
21 changes: 20 additions & 1 deletion hel/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import copy
import json


VERSION = '3.0.3'
VERSION = '3.1.0'


def parse_search_phrase(s):
Expand All @@ -27,3 +28,21 @@ def parse_search_phrase(s):
if word:
result.append(word)
return result


def update(d, nd):
if type(d) == dict and type(nd) == dict:
result = copy.copy(d)
for k, v in nd.items():
if k in d:
data = update(d[k], v)
if data is not None:
result[k] = data
else:
del result[k]
else:
if v is not None:
result[k] = v
return result
else:
return nd
58 changes: 58 additions & 0 deletions hel/utils/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ def search(pkg):

return search

def owners(param):

def search(pkg):
success = True
for rq_owner in param:
if rq_owner not in pkg['owners']:
return False
return True

return search

@_only_one_param
def license(param):
"""Search by license"""
Expand Down Expand Up @@ -236,6 +247,53 @@ def search(pkg):

return search

@_only_one_param
def q(param):

def search(pkg):
phrases = parse_search_phrase(param)
for phrase in phrases:
found = False
for k in ["name", "description", "short_description"]:
if phrase in pkg[k]:
found = True
break
if found:
continue

for k in ["owners", "authors", "license", "tags"]:
for v in pkg[k]:
if phrase in v:
found = True
break
if found:
break
if found:
continue

for k, screenshot in pkg["screenshots"].items():
if phrase in screenshot:
found = True
break
if found:
continue

for k, version in pkg["versions"].items():
if phrase in version["changes"]:
found = True
break
if found:
continue

# If we reached this line, `continue` lines weren't executed.
# It means that the phrase was not found.
# As all phrases must be found, we terminate the loop.
return False

return True

return search


class PackagesSearcher:

Expand Down
2 changes: 1 addition & 1 deletion hel/utils/tests/sample_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
name='package-1',
description='My first test package.',
short_description='1 package.',
owners=['root'],
owners=['root', 'crackes'],
authors=['root', 'Crackes'],
license='mylicense-1',
tags=['aaa', 'xxx', 'zzz'],
Expand Down
Loading

0 comments on commit 2ea5824

Please sign in to comment.