Skip to content

Commit

Permalink
Add vhost.web.eligible_for_vhost
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-mcallister committed Apr 12, 2017
1 parent 4cda0d2 commit 472fb4d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
20 changes: 20 additions & 0 deletions ocflib/vhost/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import requests

from ocflib.account.search import user_attrs
from ocflib.account.search import user_attrs_ucb

VHOST_DB_PATH = '/home/s/st/staff/vhost/vhost.conf'
VHOST_DB_URL = 'https://www.ocf.berkeley.edu/~staff/vhost.conf'

Expand Down Expand Up @@ -76,3 +79,20 @@ def has_vhost(user):
"""Returns whether or not a virtual host is already configured for
the given user."""
return any(vhost['username'] == user for vhost in get_vhosts().values())


def eligible_for_vhost(user):
"""Returns whether a user account is eligible for virtual hosting.
Currently, group accounts, faculty, and staff are eligible for virtual
hosting.
"""
attrs = user_attrs(user)
if 'callinkOid' in attrs:
return True
elif 'calnetUid' in attrs:
attrs_ucb = user_attrs_ucb(attrs['calnetUid'])
if 'EMPLOYEE-TYPE-ACADEMIC' in attrs_ucb['berkeleyEduAffiliations']:
return True

return False
9 changes: 9 additions & 0 deletions tests/vhost/web_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import mock
import pytest

from ocflib.vhost.web import eligible_for_vhost
from ocflib.vhost.web import get_vhost_db
from ocflib.vhost.web import get_vhosts
from ocflib.vhost.web import has_vhost
Expand Down Expand Up @@ -83,3 +84,11 @@ def test_proper_parse(self, mock_get_vhosts_db):
])
def test_has_vhost(self, user, should_have_vhost, mock_get_vhosts_db):
assert has_vhost(user) == should_have_vhost

@pytest.mark.parametrize('user,should_be_eligible', [
('mattmcal', False),
('ggroup', True),
('bh', True),
])
def test_eligible_for_vhost(self, user, should_be_eligible):
assert eligible_for_vhost(user) == should_be_eligible

0 comments on commit 472fb4d

Please sign in to comment.