def github():
    if not request_allowed(request.remote_addr,
                           app.config['GITHUB_PUBLIC_NETWORK'],
                           app.debug):
        abort(403)
    if request.authorization is None:
        return authenticate()
    if request.authorization.username != app.config['GITHUB_HOOK_USERNAME'] or \
       request.authorization.password != app.config['GITHUB_HOOK_PASSWORD']:
        return authenticate()
    if 'payload' not in request.form:
        abort(400)
    try:
        payload = GithubPayload(request.form['payload'])
        git = Git(app.config['GITHUB_REPO_ROOT'], payload)
        app.logger.info('Syncing repo: %s\n%s' %
                        (payload.repository_path,
                         git.sync_repo(app.config['GITHUB_AUTH_USERNAME'],
                                       app.config['GITHUB_AUTH_PASSWORD'])))
        return 'Ok\n'
    except Exception as err:
        app.logger.error('%s: %s\n%s' % (err.__class__.__name__, str(err),
                                         traceback.format_exc()))
        return 'Fail\n', 500
 def test_valid_ip(self):
     self.assertTrue(request_allowed('192.30.254.123', '192.30.252.0/22'))
 def test_loopback_without_debug(self):
     self.assertFalse(request_allowed('127.0.0.1', '192.30.252.0/22',
                                      False))
 def test_loopback(self):
     self.assertTrue(request_allowed('127.0.0.1', '192.30.252.0/22',
                                     True))
 def test_private_ip_without_debug(self):
     self.assertFalse(request_allowed('192.168.0.1', '192.30.252.0/22',
                                      False))
 def test_private_ip(self):
     self.assertTrue(request_allowed('192.168.0.1', '192.30.252.0/22',
                                     True))
 def test_invalid_ip(self):
     self.assertFalse(request_allowed('192.30.251.123', '192.30.252.0/22'))