Skip to content

Commit

Permalink
Merge pull request #2 from jonnybazookatone/master
Browse files Browse the repository at this point in the history
Cleanup of webapp and tests
  • Loading branch information
jonnybazookatone committed Jan 28, 2016
2 parents 6fff99d + f0646a5 commit ba0871d
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 102 deletions.
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ language: python
python:
- "2.7"

services:
- redis-server

install:
- "pip install -r requirements.txt"
- "pip install -r dev-requirements.txt"
- "pip install -r web-requirements.txt"
- "pip install -r dev-web-requirements.txt"

script:
- "py.test"
Expand Down
36 changes: 2 additions & 34 deletions ADSDeploy/tests/test_unit/test_webapp.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Test utilities
"""
import os

import hmac
import json
import mock
Expand All @@ -13,8 +13,7 @@
from ADSDeploy.webapp.views import GithubListener
from stub_data.stub_webapp import github_payload, payload_tag
from ADSDeploy.webapp.utils import get_boto_session
from ADSDeploy.webapp.exceptions import TimeOutError, NoSignatureInfo, InvalidSignature, \
UnknownRepoError
from ADSDeploy.webapp.exceptions import NoSignatureInfo, InvalidSignature
from flask.ext.testing import TestCase


Expand Down Expand Up @@ -128,10 +127,6 @@ def test_parse_github_payload(self):
r = FakeRequest()
r.data = github_payload.replace('"name": "adsws"', '"name": "mission-control"')

# Unknown repos should raise UnknownRepoError
with self.assertRaises(UnknownRepoError):
GithubListener.parse_github_payload(r)

# Modify the data such that the payload refers to a known repo,
# assert that the returned models.Commit contains the expected data
r.data = github_payload
Expand Down Expand Up @@ -179,33 +174,6 @@ def test_parse_github_payload_tag(self):
msg='Key "{}" not found in "{}"'.format(key, c)
)

def test_database_entry_from_payload(self):
"""
Tests that a database entry is made based on the payload received
from the parsed GitHub webhook
"""

payload = {
'repository': 'important-service',
'commit': 'da89fuhds',
'environment': 'staging',
'author': 'author',
'tag': 'da89fuhds',
}

GithubListener.push_database(payload)

entry = db.session.query(Packet).filter(Packet.commit == 'da89fuhds').one()
for key in payload:
self.assertEqual(
payload[key],
getattr(entry, key),
msg='Expected entry {} is not equal to actual entry {}'.format(
payload[key],
getattr(entry, key)
)
)

@mock.patch('ADSDeploy.webapp.views.MiniRabbit')
def test_payload_sent_to_rabbitmq(self, mocked_rabbit):
"""
Expand Down
5 changes: 1 addition & 4 deletions ADSDeploy/tests/test_unit/test_webservices.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,15 @@ def test_githublistener_endpoint(self):
self.assertStatus(r, 400) # No signature given


@mock.patch('ADSDeploy.webapp.views.GithubListener.push_database')
@mock.patch('ADSDeploy.webapp.views.GithubListener.push_rabbitmq')
@mock.patch('ADSDeploy.webapp.views.GithubListener.verify_github_signature')
def test_githublistener_forwards_message(self, mocked_gh, mocked_rabbit, mocked_db):
def test_githublistener_forwards_message(self, mocked_gh, mocked_rabbit):
"""
Test that the GitHub listener passes the posted message to the
relevant worker.
"""

mocked_gh.return_value = True
mocked_db.return_value = True

url = url_for('githublistener')

Expand All @@ -70,7 +68,6 @@ def test_githublistener_forwards_message(self, mocked_gh, mocked_rabbit, mocked_
}

mocked_rabbit.assert_called_once_with(expected_packet)
mocked_db.assert_called_once_with(expected_packet)

@mock.patch('ADSDeploy.webapp.views.GithubListener')
def test_rabbitmqlistener_forwards_message(self, mocked_gh):
Expand Down
16 changes: 0 additions & 16 deletions ADSDeploy/webapp/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,6 @@
AWS_ACCESS_KEY = 'redacted'
AWS_SECRET_KEY = 'redacted'

WATCHED_REPOS = [
'adsws',
'solr-service',
'export_service',
'graphics_service',
'recommender_service',
'citation_helper_service',
'metrics_service',
'vis-services',
'biblib-service',
'orcid-service',
'myads',
'object_service',
'harbour-service'
]

DEPLOY_LOGGING = {
'version': 1,
'disable_existing_loggers': False,
Expand Down
13 changes: 1 addition & 12 deletions ADSDeploy/webapp/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Custom exceptions
"""


class NoSignatureInfo(Exception):
"""
Raised when no signature info is found
Expand All @@ -14,19 +15,7 @@ class InvalidSignature(Exception):
"""


class UnknownRepoError(Exception):
"""
Raised when a repo is not known to mc
"""


class UnknownServiceError(Exception):
"""
Raised when a service is not known to mc
"""


class TimeOutError(Exception):
"""
Raised when a generic function does not respond after a given time
"""
38 changes: 5 additions & 33 deletions ADSDeploy/webapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
Views
"""

import pika
import hashlib
import hmac
import json
import hashlib

import pika
from ADSDeploy.config import RABBITMQ_URL
from flask import current_app, request, abort
from flask.ext.restful import Resource
from .models import db, Packet
from .exceptions import NoSignatureInfo, InvalidSignature, UnknownRepoError
from ADSDeploy.config import RABBITMQ_URL

from .exceptions import NoSignatureInfo, InvalidSignature


class MiniRabbit(object):
Expand Down Expand Up @@ -206,28 +206,6 @@ def push_rabbitmq(payload, **kwargs):
payload=json.dumps(payload)
)

@staticmethod
def push_database(payload):
"""
Puts a database entry into the backend data store using the GitHub
payload.
:param payload: payload containing relevant attributes for db entry
:type payload: dict
"""

packet = Packet(
commit=payload['commit'],
tag=payload['tag'],
author=payload['author'],
repository=payload['repository'],
environment=payload['environment']
)

db.session.add(packet)
db.session.commit()

@staticmethod
def parse_github_payload(request=None):
"""
Expand All @@ -252,9 +230,6 @@ def parse_github_payload(request=None):
if 'tags' in formatted_request['ref'] else None
}

if payload['repository'] not in current_app.config.get('WATCHED_REPOS'):
raise UnknownRepoError("{}".format(payload['repository']))

return payload

def post(self):
Expand All @@ -277,9 +252,6 @@ def post(self):
except UnknownRepoError, e:
return {"Unknown repo": "{}".format(e)}, 400

# Put a DB entry
GithubListener.push_database(payload)

# Submit to RabbitMQ worker
GithubListener.push_rabbitmq(payload)

Expand Down

0 comments on commit ba0871d

Please sign in to comment.