<s> from django import http <EOL> from django . contrib import messages <EOL> from django . core . urlresolvers import reverse <EOL> from django_openstack import api <EOL> from django_openstack . tests . view_tests import base <EOL> from openstackx . api import exceptions as api_exceptions <EOL> from mox import IsA <EOL> class AuthViewTests ( base . BaseViewTests ) : <EOL> def setUp ( self ) : <EOL> super ( AuthViewTests , self ) . setUp ( ) <EOL> self . setActiveUser ( ) <EOL> self . PASSWORD = '<STR_LIT>' <EOL> def test_login_index ( self ) : <EOL> res = self . client . get ( reverse ( '<STR_LIT>' ) ) <EOL> self . assertTemplateUsed ( res , '<STR_LIT>' ) <EOL> def test_login_user_logged_in ( self ) : <EOL> self . setActiveUser ( self . TEST_TOKEN , self . TEST_USER , self . TEST_TENANT , <EOL> False , self . TEST_SERVICE_CATALOG ) <EOL> res = self . client . get ( reverse ( '<STR_LIT>' ) ) <EOL> self . assertRedirectsNoFollow ( res , reverse ( '<STR_LIT>' ) ) <EOL> def test_login_admin_logged_in ( self ) : <EOL> self . setActiveUser ( self . TEST_TOKEN , self . TEST_USER , self . TEST_TENANT , <EOL> True , self . TEST_SERVICE_CATALOG ) <EOL> res = self . client . get ( reverse ( '<STR_LIT>' ) ) <EOL> self . assertRedirectsNoFollow ( res , reverse ( '<STR_LIT>' ) ) <EOL> def test_login_no_tenants ( self ) : <EOL> NEW_TENANT_ID = '<STR_LIT>' <EOL> NEW_TENANT_NAME = '<STR_LIT>' <EOL> TOKEN_ID = <NUM_LIT:1> <EOL> form_data = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : self . PASSWORD , <EOL> '<STR_LIT:username>' : self . TEST_USER } <EOL> self . mox . StubOutWithMock ( api , '<STR_LIT>' ) <EOL> aToken = self . mox . CreateMock ( api . Token ) <EOL> aToken . id = TOKEN_ID <EOL> aToken . user = { '<STR_LIT>' : [ { '<STR_LIT:name>' : '<STR_LIT>' } ] } <EOL> aToken . serviceCatalog = { } <EOL> api . token_create ( IsA ( http . HttpRequest ) , "<STR_LIT>" , self . TEST_USER , <EOL> self . PASSWORD ) . AndReturn ( aToken ) <EOL> aTenant = self . mox . CreateMock ( api . Token ) <EOL> aTenant . id = NEW_TENANT_ID <EOL> aTenant . name = NEW_TENANT_NAME <EOL> self . mox . StubOutWithMock ( api , '<STR_LIT>' ) <EOL> api . tenant_list_for_token ( IsA ( http . HttpRequest ) , aToken . id ) . AndReturn ( [ ] ) <EOL> self . mox . StubOutWithMock ( messages , '<STR_LIT:error>' ) <EOL> messages . error ( IsA ( http . HttpRequest ) , IsA ( unicode ) ) <EOL> self . mox . ReplayAll ( ) <EOL> res = self . client . post ( reverse ( '<STR_LIT>' ) , form_data ) <EOL> self . assertTemplateUsed ( res , '<STR_LIT>' ) <EOL> self . mox . VerifyAll ( ) <EOL> def test_login ( self ) : <EOL> NEW_TENANT_ID = '<STR_LIT>' <EOL> NEW_TENANT_NAME = '<STR_LIT>' <EOL> TOKEN_ID = <NUM_LIT:1> <EOL> form_data = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : self . PASSWORD , <EOL> '<STR_LIT:username>' : self . TEST_USER } <EOL> self . mox . StubOutWithMock ( api , '<STR_LIT>' ) <EOL> aToken = self . mox . CreateMock ( api . Token ) <EOL> aToken . id = TOKEN_ID <EOL> aToken . user = { '<STR_LIT>' : [ { '<STR_LIT:name>' : '<STR_LIT>' } ] } <EOL> aToken . serviceCatalog = { } <EOL> api . token_create ( IsA ( http . HttpRequest ) , "<STR_LIT>" , self . TEST_USER , <EOL> self . PASSWORD ) . AndReturn ( aToken ) <EOL> aTenant = self . mox . CreateMock ( api . Token ) <EOL> aTenant . id = NEW_TENANT_ID <EOL> aTenant . name = NEW_TENANT_NAME <EOL> self . mox . StubOutWithMock ( api , '<STR_LIT>' ) <EOL> api . tenant_list_for_token ( IsA ( http . HttpRequest ) , aToken . id ) . AndReturn ( [ aTenant ] ) <EOL> self . mox . StubOutWithMock ( api , '<STR_LIT>' ) <EOL> api . token_create_scoped_with_token ( IsA ( http . HttpRequest ) , aTenant . id , <EOL> aToken . id ) . AndReturn ( aToken ) <EOL> self . mox . ReplayAll ( ) <EOL> res = self . client . post ( reverse ( '<STR_LIT>' ) , form_data ) <EOL> self . assertRedirectsNoFollow ( res , reverse ( '<STR_LIT>' ) ) <EOL> self . mox . VerifyAll ( ) <EOL> def test_login_invalid_credentials ( self ) : <EOL> form_data = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : self . PASSWORD , <EOL> '<STR_LIT:username>' : self . TEST_USER } <EOL> self . mox . StubOutWithMock ( api , '<STR_LIT>' ) <EOL> unauthorized = api_exceptions . Unauthorized ( '<STR_LIT>' , message = '<STR_LIT>' ) <EOL> api . token_create ( IsA ( http . HttpRequest ) , "<STR_LIT>" , self . TEST_USER , <EOL> self . PASSWORD ) . AndRaise ( unauthorized ) <EOL> self . mox . ReplayAll ( ) <EOL> res = self . client . post ( reverse ( '<STR_LIT>' ) , form_data ) <EOL> self . assertTemplateUsed ( res , '<STR_LIT>' ) <EOL> self . mox . VerifyAll ( ) <EOL> def test_login_exception ( self ) : <EOL> form_data = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : self . PASSWORD , <EOL> '<STR_LIT:username>' : self . TEST_USER } <EOL> self . mox . StubOutWithMock ( api , '<STR_LIT>' ) <EOL> api_exception = api_exceptions . ApiException ( '<STR_LIT>' , <EOL> message = '<STR_LIT>' ) <EOL> api . token_create ( IsA ( http . HttpRequest ) , "<STR_LIT>" , self . TEST_USER , <EOL> self . PASSWORD ) . AndRaise ( api_exception ) <EOL> self . mox . ReplayAll ( ) <EOL> res = self . client . post ( reverse ( '<STR_LIT>' ) , form_data ) <EOL> self . assertTemplateUsed ( res , '<STR_LIT>' ) <EOL> self . mox . VerifyAll ( ) <EOL> def test_switch_tenants_index ( self ) : <EOL> res = self . client . get ( reverse ( '<STR_LIT>' , args = [ self . TEST_TENANT ] ) ) <EOL> self . assertTemplateUsed ( res , '<STR_LIT>' ) <EOL> def test_switch_tenants ( self ) : <EOL> NEW_TENANT_ID = '<STR_LIT>' <EOL> NEW_TENANT_NAME = '<STR_LIT>' <EOL> TOKEN_ID = <NUM_LIT:1> <EOL> self . setActiveUser ( self . TEST_TOKEN , self . TEST_USER , self . TEST_TENANT , <EOL> False , self . TEST_SERVICE_CATALOG ) <EOL> form_data = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : self . PASSWORD , <EOL> '<STR_LIT>' : NEW_TENANT_ID , <EOL> '<STR_LIT:username>' : self . TEST_USER } <EOL> self . mox . StubOutWithMock ( api , '<STR_LIT>' ) <EOL> aTenant = self . mox . CreateMock ( api . Token ) <EOL> aTenant . id = NEW_TENANT_ID <EOL> aTenant . name = NEW_TENANT_NAME <EOL> aToken = self . mox . CreateMock ( api . Token ) <EOL> aToken . id = TOKEN_ID <EOL> aToken . user = { '<STR_LIT>' : [ { '<STR_LIT:name>' : '<STR_LIT>' } ] } <EOL> aToken . serviceCatalog = { } <EOL> api . token_create ( IsA ( http . HttpRequest ) , NEW_TENANT_ID , self . TEST_USER , <EOL> self . PASSWORD ) . AndReturn ( aToken ) <EOL> self . mox . StubOutWithMock ( api , '<STR_LIT>' ) <EOL> api . tenant_list_for_token ( IsA ( http . HttpRequest ) , aToken . id ) . AndReturn ( [ aTenant ] ) <EOL> self . mox . ReplayAll ( ) <EOL> res = self . client . post ( reverse ( '<STR_LIT>' , args = [ NEW_TENANT_ID ] ) , <EOL> form_data ) <EOL> self . assertRedirectsNoFollow ( res , reverse ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( self . client . session [ '<STR_LIT>' ] , NEW_TENANT_NAME ) <EOL> self . mox . VerifyAll ( ) <EOL> def test_logout ( self ) : <EOL> KEY = '<STR_LIT>' <EOL> VALUE = '<STR_LIT>' <EOL> self . assertNotIn ( KEY , self . client . session ) <EOL> self . client . session [ KEY ] = VALUE <EOL> res = self . client . get ( reverse ( '<STR_LIT>' ) ) <EOL> self . assertRedirectsNoFollow ( res , reverse ( '<STR_LIT>' ) ) <EOL> self . assertNotIn ( KEY , self . client . session ) </s>
