Skip to content

Commit

Permalink
start testing for download feature
Browse files Browse the repository at this point in the history
  • Loading branch information
armish committed Aug 28, 2015
1 parent ccc092e commit b75c23f
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
31 changes: 31 additions & 0 deletions cycledash/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from flask.ext.login import login_user, logout_user
from sqlalchemy import exc
import voluptuous
import base64

from cycledash import db, bcrypt, login_manager
from cycledash.helpers import prepare_request_data, safely_redirect_to_next
Expand Down Expand Up @@ -107,3 +108,33 @@ def register():
login_user(user)
return redirect('/')
return render_template('register.html', errors=errors)

def check_login_from_key(api_key):
username, password = api_key.split(":")
return check_login(username, password)

def load_user_from_request(request):
"""Support for basic authorization."""
# first, try to login using the api_key url arg
api_key = request.args.get('api_key')
if api_key:
user = check_login_from_key(api_key)
if user:
return user

# next, try to login using Basic Auth
api_key = request.headers.get('Authorization')
if api_key:
api_key = api_key.replace('Basic ', '', 1)
try:
api_key = base64.b64decode(api_key)
except TypeError:
pass
user = check_login_from_key(api_key)
if user:
return user

# finally, return None if both methods did not login the user
return None

login_manager.request_loader(load_user_from_request)
46 changes: 46 additions & 0 deletions tests/python/test_download.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import mock
import nose
import json

from cycledash import app, db
from common.helpers import tables

from test_projects_api import create_project_with_name
from test_bams_api import create_bam_with_name
from test_runs_api import create_run_with_uri
from workers.genotype_extractor import _extract

import helpers

class TestDownload(helpers.ResourceTest):
""" Creates a project from a test file, extracts the genotype
and tries to download it back. """

@classmethod
def setUpClass(cls):
cls.project = create_project_with_name('Downloadable project')
cls.run = create_run_with_uri(cls.project['id'],
'/tests/data/somatic_hg19_14muts.vcf')
_extract(cls.run['id'])
return super(TestDownload, cls).setUpClass()

def tearDown(self):
with tables(db.engine, 'projects', 'vcfs', 'genotypes') \
as (con, projects, runs, genotypes):
genotypes.delete().execute()
runs.delete().execute()
projects.delete().execute()

def test_download(self):
# match the URL with the default download button href
downUrl = ("/runs/" + str(self.run['id']) + "/download?query="
"%7B%22range%22%3A%7B%22start%22%3Anull%2C%22end%22%3Anull"
"%2C%22contig%22%3Anull%7D%2C%22filters%22%3A%5B%5D%2C%22"
"sortBy%22%3A%5B%7B%22order%22%3A%22asc%22%2C%22"
"columnName%22%3A%22contig%22%7D%2C%7B%22order"
"%22%3A%22asc%22%2C%22columnName%22%3A%22position"
"%22%7D%5D%2C%22page%22%3A0%2C%22limit%22%3A250%2C%22"
"compareToVcfId%22%3Anull%7D")
r = self.get(downUrl)
assert r.status_code == 200 # no fail
assert len(r.data) == 1193 # might differ from the original file

0 comments on commit b75c23f

Please sign in to comment.