def setUp(self): self.config = testing.setUp() from sqlalchemy import create_engine engine = create_engine('sqlite://') from consensus.models import ( Base, User, ) DBSession.configure(bind=engine) Base.metadata.create_all(engine) with transaction.manager: role = Role('ROLE_USER', 'The default role for all users.') DBSession.add(role)
def signup(request): try: username = request.POST.getone('username') password = request.POST.getone('password') except KeyError: return HTTPBadRequest() new_user = User(username, str(bkrypt.Password.create(password))) role_user = DBSession.query(Role).filter_by(alias='ROLE_USER').first() try: with transaction.manager: DBSession.add(new_user) user = DBSession.query(User).filter_by(username=username).first() user.roles.append(role_user) except IntegrityError: return HTTPBadRequest() return HTTPFound(location=request.route_url('login'))
def create_election(request): if (not is_authenticated(request)): return HTTPUnauthorized() try: election_name = request.POST.getone('name') election_desc = request.POST.getone('body') method = request.POST.getone('method') except KeyError: return HTTPBadRequest() read_method = DBSession.query(Method).filter_by(python_name=method).first() if (read_method is None): return HTTPBadRequest() election = Election(election_name, election_desc, read_method) with transaction.manager: new_id = DBSession.add(election) return HTTPFound(location=request.route_url('view_election', election_id=new_id))
def test_view_election(self): request = self._get_request() request.POST['username'] = '******' request.POST['password'] = '******' response = auth(request) auth_session = request.session self.assertTrue(auth_session['authentication'].is_authenticated()) request = self._get_request() request.session = auth_session request.POST['name'] = 'Test' request.POST['body'] = 'An election for testing.' request.POST['method'] = 'TestMethod' response = create_election(request) self.assertEqual(response.status_int, 302) requet = self._get_request() request.session = auth_session response = view_all_elections(request) self.assertEqual(len(response), 3) self.assertEqual(response['Test']['name'], 'Test') self._get_request() request.session = auth_session request.url = response['Test']['view_url'] uuid = DBSession.query(Election).filter_by(name='Test').first().id.urn request.matchdict['election_id'] = uuid response = view_election(request) self.assertEqual(response['name'], 'Test') self.assertEqual(response['body'], 'An election for testing.') self.assertEqual(response['method']['name'], 'Test Method')
def view_all_elections(request): if (not is_authenticated(request)): return HTTPUnauthorized() elections = DBSession.query(Election).all() result = {} for election in elections: election_url = request.route_url('view_election',election_id=election.id.urn) name = election.name result[election.name] = { 'view_url' : election_url, \ 'name' : name } result['page_name'] = 'All Elections' result['elections'] = elections return result
def setUp(self): self.config = testing.setUp() self.auth_strategy = AuthenticationStrategy() from sqlalchemy import create_engine engine = create_engine('sqlite://') from consensus.models import ( Base, User, ) DBSession.configure(bind=engine) Base.metadata.create_all(engine) with transaction.manager: ballot_method = Method('TestMethod','Test Method','A test balloting method') model = User('TestUser', str(Password.create('TestPass'))) role = Role('ROLE_USER', 'The default role for all users.') DBSession.add(ballot_method) DBSession.add(role) model.roles.append(DBSession.query(Role).filter_by(alias='ROLE_USER').first()) DBSession.add(model)
def view_election(request): if (not is_authenticated(request)): return HTTPUnauthorized() try: guid = uuid.UUID(request.matchdict['election_id']) except KeyError: return HTTPBadRequest() election_id = guid election = DBSession.query(Election).filter_by(id=guid).first() return { 'id' : election.id.urn, \ 'page_name' : election.name, \ 'name' : election.name, \ 'body' : election.body, \ 'method' : { 'name' : election.method.name, \ 'description' : election.method.description } }
def authenticate(self, request): try: supplied_username = request.POST.getone('username') password = request.POST.getone('password') except KeyError: raise AuthenticationError() user = DBSession.query(User).filter_by(username=supplied_username).first() if (user is None): raise AuthenticationError() p = Password(user.password) if (p == password): return Authentication(user,user.roles) else: raise AuthenticationError()
def tearDown(self): DBSession.remove() testing.tearDown()