Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cli project features #337

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions infrabox/test/api/collaborators_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_collaborators_root(self):
r = TestClient.post('api/v1/projects/%s/collaborators' % self.project_id,
data=self.test_collaborator_data,
headers=TestClient.get_user_authorization(self.user_id))
self.assertEqual(r['message'], 'Successfully added user')
self.assertEqual(r['message'], 'Successfully added user.')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no . at the end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this to keep consistency across the code. Since some requests return with dot at the end and some without. We can consider to remove dots at the end everywhere, but always add a dot in the logger.

What do you think?

self.assertEqual(r['status'], 200)

# test unauthorized
Expand Down Expand Up @@ -54,7 +54,7 @@ def test_collaborators_delete(self):
% (self.project_id, self.collaborator_id),
headers=TestClient.get_user_authorization(self.user_id))

self.assertEqual(r['message'], 'Successfully removed user')
self.assertEqual(r['message'], 'Successfully removed user.')
self.assertEqual(r['status'], 200)

# check if database does not contain this collaborator anymore
Expand Down
8 changes: 4 additions & 4 deletions infrabox/test/api/secrets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ def test_secrets_root(self):
# test secret creation
r = TestClient.post('api/v1/projects/%s/secrets/' % self.project_id, data=self.test_secret_data,
headers=TestClient.get_user_authorization(self.user_id))
self.assertEqual(r['message'], 'Successfully added secret')
self.assertEqual(r['message'], 'Successfully added secret.')
self.assertEqual(r['status'], 200)

# test invalid secret data handling
for invalid_secret_data in self.invalid_test_secret_data:
r = TestClient.post('api/v1/projects/%s/secrets/' % self.project_id, data=invalid_secret_data,
headers=TestClient.get_user_authorization(self.user_id))
self.assertEqual(r['message'], 'Secret name must be not empty alphanumeric string')
self.assertEqual(r['message'], 'Secret name must be not empty alphanumeric string.')

# test name already exist
r = TestClient.post('api/v1/projects/%s/secrets/' % self.project_id, data=self.test_secret_data,
headers=TestClient.get_user_authorization(self.user_id))
self.assertEqual(r['message'], 'Secret with this name already exist')
self.assertEqual(r['message'], 'Secret with this name already exist.')

# test secret receiving
r = TestClient.get('api/v1/projects/%s/secrets' % self.project_id,
Expand All @@ -61,7 +61,7 @@ def test_secret_delete(self):
r = TestClient.delete('api/v1/projects/%s/secrets/%s' % (self.project_id, secret_id),
headers=TestClient.get_user_authorization(self.user_id))

self.assertEqual(r['message'], 'Successfully deleted secret')
self.assertEqual(r['message'], 'Successfully deleted secret.')
self.assertEqual(r['status'], 200)

r = TestClient.execute_one("""SELECT count(*) FROM secret WHERE id = %s""", [secret_id])
Expand Down
4 changes: 2 additions & 2 deletions infrabox/test/api/tokens_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_tokens_root(self):
# test token creation
r = TestClient.post('api/v1/projects/%s/tokens' % self.project_id, data=self.test_token_data,
headers=TestClient.get_user_authorization(self.user_id))
self.assertEqual(r['message'], 'Successfully added token')
self.assertEqual(r['message'], 'Successfully added token.')
self.assertEqual(r['status'], 200)

# test token receiving
Expand All @@ -47,7 +47,7 @@ def test_tokens_delete(self):
r = TestClient.delete('api/v1/projects/%s/tokens/%s' % (self.project_id, token_id),
headers=TestClient.get_user_authorization(self.user_id))

self.assertEqual(r['message'], 'Successfully deleted token')
self.assertEqual(r['message'], 'Successfully deleted token.')
self.assertEqual(r['status'], 200)

r = TestClient.execute_one("""SELECT count(*) FROM auth_token WHERE id = '%s'""" % token_id)
Expand Down
8 changes: 4 additions & 4 deletions src/api/handlers/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ class Project(Resource):
@auth_required(['user', 'project'])
@api.marshal_with(project_model)
def get(self, project_id):
p = g.db.execute_one_dict('''
p = g.db.execute_one_dict("""
SELECT name, id, type, public
FROM project
WHERE id = %s
''', [project_id])
""", [project_id])
return p

@ns.route('/<project_id>/state.svg')
Expand All @@ -73,9 +73,9 @@ class State(Resource):

@nocache
def get(self, project_id):
p = g.db.execute_one_dict('''
p = g.db.execute_one_dict("""
SELECT type FROM project WHERE id = %s
''', [project_id])
""", [project_id])

project_type = p['type']

Expand Down
4 changes: 2 additions & 2 deletions src/api/handlers/projects/collaborators.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def post(self, project_id):

g.db.commit()

return OK('Successfully added user')
return OK('Successfully added user.')


@ns.route('/<project_id>/collaborators/<user_id>')
Expand Down Expand Up @@ -102,4 +102,4 @@ def delete(self, project_id, user_id):

g.db.commit()

return OK('Successfully removed user')
return OK('Successfully removed user.')
24 changes: 14 additions & 10 deletions src/api/handlers/projects/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from flask_restplus import Resource, fields

from pyinfraboxutils import get_logger
from pyinfrabox.utils import validate_uuid4
from pyinfraboxutils.ibrestplus import api
from pyinfraboxutils.ibflask import auth_required, OK

Expand All @@ -16,7 +17,8 @@
project_model = api.model('Project', {
'id': fields.String(required=True),
'name': fields.String(required=True),
'type': fields.String(required=True)
'type': fields.String(required=True),
'public': fields.String(required=True)
})

add_project_schema = {
Expand All @@ -38,13 +40,14 @@ class Projects(Resource):
@auth_required(['user'], check_project_access=False)
@api.marshal_list_with(project_model)
def get(self):
projects = g.db.execute_many_dict('''
SELECT p.id, p.name, p.type FROM project p
projects = g.db.execute_many_dict("""
SELECT p.id, p.name, p.type, p.public
FROM project p
INNER JOIN collaborator co
ON co.project_id = p.id
AND %s = co.user_id
ORDER BY p.name
''', [g.token['user']['id']])
""", [g.token['user']['id']])

return projects

Expand Down Expand Up @@ -200,7 +203,7 @@ class ProjectName(Resource):
@api.marshal_with(project_model)
def get(self, project_name):
project = g.db.execute_one_dict('''
SELECT id, name, type
SELECT id, name, type, public
FROM project
WHERE name = %s
''', [project_name])
Expand All @@ -218,7 +221,7 @@ class Project(Resource):
@api.marshal_with(project_model)
def get(self, project_id):
project = g.db.execute_one_dict('''
SELECT p.id, p.name, p.type
SELECT p.id, p.name, p.type, p.public
FROM project p
WHERE id = %s
''', [project_id])
Expand All @@ -227,14 +230,15 @@ def get(self, project_id):

@auth_required(['user'], check_project_owner=True)
def delete(self, project_id):
if not validate_uuid4(project_id):
abort(400, "Invalid project uuid.")

project = g.db.execute_one_dict('''
project = g.db.execute_one_dict("""
DELETE FROM project WHERE id = %s RETURNING type
''', [project_id])
""", [project_id])

if not project:
abort(404)

abort(400, 'Project with such an id does not exist.')

if project['type'] == 'github':
repo = g.db.execute_one_dict('''
Expand Down
38 changes: 25 additions & 13 deletions src/api/handlers/projects/secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from flask_restplus import Resource, fields
import re

from pyinfrabox.utils import validate_uuid4
from pyinfraboxutils.ibflask import auth_required, OK
from pyinfraboxutils.ibrestplus import api
from api.namespaces import project as ns
Expand Down Expand Up @@ -37,39 +38,50 @@ def post(self, project_id):
b = request.get_json()

if not Secrets.name_pattern.match(b['name']):
abort(400, 'Secret name must be not empty alphanumeric string')
abort(400, 'Secret name must be not empty alphanumeric string.')

result = g.db.execute_one_dict('''
result = g.db.execute_one_dict("""
SELECT COUNT(*) as cnt FROM secret WHERE project_id = %s
''', [project_id])
""", [project_id])

if result['cnt'] > 50:
abort(400, 'Too many secrets')
abort(400, 'Too many secrets.')

r = g.db.execute_one('''
r = g.db.execute_one("""
SELECT count(*) FROM secret
WHERE project_id = %s AND name = %s
''', [project_id, b['name']])
""", [project_id, b['name']])

if r[0] > 0:
abort(400, 'Secret with this name already exist')
abort(400, 'Secret with this name already exist.')

g.db.execute('''
g.db.execute("""
INSERT INTO secret (project_id, name, value) VALUES(%s, %s, %s)
''', [project_id, b['name'], b['value']])
""", [project_id, b['name'], b['value']])

g.db.commit()

return OK('Successfully added secret')
return OK('Successfully added secret.')


@ns.route('/<project_id>/secrets/<secret_id>')
class Secret(Resource):
@auth_required(['user'])
def delete(self, project_id, secret_id):
g.db.execute('''
if not validate_uuid4(secret_id):
abort(400, "Invalid secret uuid.")

num_secrets = g.db.execute_one("""
SELECT COUNT(*) FROM secret
WHERE project_id = %s and id = %s
""", [project_id, secret_id])[0]

if num_secrets == 0:
return abort(400, 'Such secret does not exist.')

g.db.execute("""
DELETE FROM secret WHERE project_id = %s and id = %s
''', [project_id, secret_id])
""", [project_id, secret_id])
g.db.commit()

return OK('Successfully deleted secret')
return OK('Successfully deleted secret.')
6 changes: 3 additions & 3 deletions src/api/handlers/projects/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ def post(self, project_id):

g.db.commit()

return OK('Successfully added token', {'token': token})
return OK('Successfully added token.', {'token': token})

@ns.route('/<project_id>/tokens/<token_id>')
class Token(Resource):

@auth_required(['user'])
def delete(self, project_id, token_id):
if not validate_uuid4(token_id):
abort(400, "Invalid project-token uuid")
abort(400, "Invalid project-token uuid.")

num_tokens = g.db.execute_one("""
SELECT COUNT(*) FROM auth_token
Expand All @@ -75,4 +75,4 @@ def delete(self, project_id, token_id):
""", [project_id, token_id])
g.db.commit()

return OK('Successfully deleted token')
return OK('Successfully deleted token.')