File: test_SessionFactory.py

package info (click to toggle)
mobyle 1.5.5%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,288 kB
  • sloc: python: 22,709; makefile: 35; sh: 33; ansic: 10; xml: 6
file content (131 lines) | stat: -rw-r--r-- 6,239 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
########################################################################################
#                                                                                      #
#   Author: Bertrand Neron,                                                            #
#   Organization:'Biological Software and Databases' Group, Institut Pasteur, Paris.   #  
#   Distributed under GPLv2 Licence. Please refer to the COPYING.LIB document.         #
#                                                                                      #
########################################################################################

import unittest2 as unittest
import os
import sys
import shutil

MOBYLEHOME = os.path.abspath( os.path.join( os.path.dirname( __file__ ) , "../../../" ) )
os.environ[ 'MOBYLEHOME' ] = MOBYLEHOME
if ( MOBYLEHOME ) not in sys.path:
    sys.path.append( MOBYLEHOME )
if ( os.path.join( MOBYLEHOME , 'Src' ) ) not in sys.path:    
    sys.path.append( os.path.join( MOBYLEHOME , 'Src' ) )
    
import Mobyle.Test.MobyleTest 
from Mobyle.SessionFactory import SessionFactory
from Mobyle.MobyleError import SessionError , AuthenticationError
from Mobyle.Net import Email , EmailAddress
Email.send = lambda templateName , subject , msg : None

class SessionFactoryTest(unittest.TestCase):
    
    
    def setUp(self):
        """ 
        setting up the configuration for the test, including:
          - test configuration that does not check dns or try to send confirmation emails
        """
        self.cfg = Mobyle.ConfigManager.Config()
        self.cfg._authenticated_session = 'yes'

        shutil.rmtree( self.cfg.test_dir , ignore_errors=True )
        os.makedirs( self.cfg.test_dir )

        self.sessionFactory = SessionFactory(self.cfg)
        self.sessionFactory._SessionFactory__sessions = {}
        
    def tearDown( self ):
        self.sessionFactory._SessionFactory__sessions = {}
        shutil.rmtree( self.cfg.test_dir , ignore_errors=True )
        
    def testAnonymousSessionNormalLifeCycle(self):
        ## Creation should fail if disabled
        self.cfg._anonymous_session = 'no'
        self.assertRaises( SessionError , self.sessionFactory.getAnonymousSession )
        ## Creation should succeed if enabled
        self.cfg._anonymous_session = 'yes'
        s1 = self.sessionFactory.getAnonymousSession()
        ## Fetch an existing session
        sessionID = s1.getKey()
        s2 = self.sessionFactory.getAnonymousSession( key = sessionID ) 
        self.assertEqual( s1 , s2 )
        ## Access should fail if missing
        self.sessionFactory.removeSession( sessionID )
        self.assertRaises( SessionError , self.sessionFactory.getAnonymousSession , key = sessionID )

    def testGetInvalidAnonymousSession(self):
        self.assertRaises( SessionError, self.sessionFactory.getAnonymousSession, "__invalid_key__")
    
    def testAuthenticatedSessionNormalLifeCycle(self):
        email = Mobyle.Net.EmailAddress( 'toto@123.com' )
        password = 'tutu'
        #test 
        self.assertRaises( SessionError, self.sessionFactory.getAuthenticatedSession, email , password )
        
        #create 
        s1 = self.sessionFactory.createAuthenticatedSession( email , password )
        sessionID = s1.getKey()
        
        #creation with same email , passwd
        self.assertRaises( AuthenticationError , self.sessionFactory.createAuthenticatedSession , email , password )
    
        s2 = self.sessionFactory.getAuthenticatedSession( email , password)
        self.assertEqual( s1 , s2 )
        self.sessionFactory.removeSession( sessionID )
        self.assertRaises( AuthenticationError , self.sessionFactory.getAuthenticatedSession, email , password )
        self.cfg._authenticated_session = 'no'
        self.assertRaises( SessionError , self.sessionFactory.createAuthenticatedSession , email , password )
        self.cfg._authenticated_session = 'yes'
        
    def testAuthenticatedSessionBadEmail(self):
        self.cfg._authenticated_session = 'email'
        To = 'toto@123.com'
        email1 = Mobyle.Net.EmailAddress( To )
        email2 = Mobyle.Net.EmailAddress( To + '.uk' )
        password = 'tutu'
        sess = self.sessionFactory.createAuthenticatedSession( email1 , password )
        self.assertRaises( AuthenticationError, self.sessionFactory.getAuthenticatedSession , email2 , password  )
        self.sessionFactory.removeSession( sess.getKey() )
        
    def testAuthenticatedSessionBadPasswd(self):
        email = Mobyle.Net.EmailAddress( 'toto@123.com' )
        password = 'tutu'
        sess = self.sessionFactory.createAuthenticatedSession( email , password )
        self.assertRaises( AuthenticationError , self.sessionFactory.getAuthenticatedSession , email , password + 'bad')
        self.sessionFactory.removeSession( sess.getKey() )        
 
    
    def testRemoveSession(self):
        email = Mobyle.Net.EmailAddress( 'toto@123.com' )
        password = 'tutu'
        sess = self.sessionFactory.createAuthenticatedSession( email , password )
        sessionDir = sess.getDir()
        sessKey = sess.getKey()
        self.sessionFactory.removeSession( sessKey )
        self.assertFalse( os.path.exists( sessionDir ) )
    
    def testOpenIdAuthenticatedSession(self):
        userEmailAddr = Mobyle.Net.EmailAddress( 'toto@123.com' )
        password = 'tutu'
        self.assertRaises( AuthenticationError, self.sessionFactory.getOpenIdAuthenticatedSession, userEmailAddr )
        self.cfg._authenticated_session = 'email'
        sess = self.sessionFactory.createAuthenticatedSession( userEmailAddr , password )
        ticket_id = sess.ticket_id
        session2 = self.sessionFactory.getOpenIdAuthenticatedSession( userEmailAddr , ticket_id=ticket_id )
        self.assertEqual( sess, session2 )
        
        #retirieve an existing session but not stored in factory
        del( self.sessionFactory._SessionFactory__sessions[ sess.getKey() ] )
        session3 = self.sessionFactory.getOpenIdAuthenticatedSession( userEmailAddr , ticket_id=ticket_id )
        self.assertEqual( sess.getDir() , session3.getDir() )
        
        
if __name__ == '__main__':
    unittest.main()