Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

"""Test the Auth model setup""" 

from unittest import TestCase 

from nose.tools import eq_ 

from nose.tools import ok_ 

from pyramid import testing 

 

 

from bookie.models import DBSession 

from bookie.models.auth import Activation 

from bookie.models.auth import User 

from bookie.models.auth import UserMgr 

 

from bookie.tests import empty_db 

from bookie.tests import gen_random_word 

from bookie.tests import TestDBBase 

 

 

class TestPassword(TestCase): 

    """Test password checks""" 

    pass 

 

 

class TestAuthUser(TestCase): 

    """Test User Model""" 

    test_hash = '$2a$10$FMFKEYqC7kifFTm05iag7etE17Q0AyKvtX88XUdUcM7rvpz48He92' 

    test_password = 'testing' 

 

    def test_password_set(self): 

        """Make sure we get the proper hashed password""" 

        tst = User() 

        tst.password = self.test_password 

 

        eq_(len(tst.password), 60, 

            "Hashed should be 60 char long: " + tst.password) 

        eq_('$2a$', tst.password[:4], 

            "Hash should start with the right complexity: " + tst.password[:4]) 

 

    def test_password_match(self): 

        """Try to match a given hash""" 

 

        tst = User() 

        tst._password = self.test_hash 

 

        ok_(tst._password == self.test_hash, "Setting should have hash") 

        ok_(tst.password == self.test_hash, "Getting should have hash") 

        ok_(tst.validate_password(self.test_password), 

            "The password should pass against the given hash: " + tst.password) 

 

 

class TestAuthUserDB(TestDBBase): 

    def setUp(self): 

        """Setup Tests""" 

        from pyramid.paster import get_app 

        from bookie.tests import BOOKIE_TEST_INI 

        app = get_app(BOOKIE_TEST_INI, 'bookie') 

        from webtest import TestApp 

        self.testapp = TestApp(app) 

        testing.setUp() 

 

    def tearDown(self): 

        """Tear down each test""" 

        testing.tearDown() 

        empty_db() 

 

    def test_activation_delete(self): 

        """Make sure removing an activation does not remove a user.""" 

        tst = User() 

        tst.username = gen_random_word(10) 

        tst.activation = Activation('signup') 

        DBSession.add(tst) 

        DBSession.flush() 

 

        DBSession.delete(tst.activation) 

 

        users = UserMgr.get_list() 

 

        # We still have the admin user as well so the count is two. 

        eq_( 

            2, 

            len(users), 

            'We should have a total of 2 users still: ' + str(len(users))) 

 

    def test_activation_cascade(self): 

        """Removing a user cascades the activations as well.""" 

        tst = User() 

        tst.username = gen_random_word(10) 

        tst.activation = Activation('signup') 

        DBSession.add(tst) 

        DBSession.flush() 

 

        DBSession.delete(tst) 

 

        users = UserMgr.get_list() 

 

        # We still have the admin user as well so the count is one. 

        eq_( 

            1, 

            len(users), 

            'We should have a total of 1 user still: ' + str(len(users))) 

 

        activations = DBSession.query(Activation).all() 

        eq_(0, len(activations), 'There should be no activations left') 

 

 

 

class TestAuthMgr(TestCase): 

    """Test User Manager""" 

 

    def test_get_id(self): 

        """Fetching user by the id""" 

        # the migration adds an initial admin user to the system 

        user = UserMgr.get(user_id=1) 

        eq_(user.id, 1, 

            "Should have a user id of 1: " + str(user.id)) 

        eq_(user.username, 'admin', 

            "Should have a username of admin: " + user.username) 

 

    def test_get_username(self): 

        """Fetching the user by the username""" 

        user = UserMgr.get(username='admin') 

        eq_(user.id, 1, 

            "Should have a user id of 1: " + str(user.id)) 

        eq_(user.username, 'admin', 

            "Should have a username of admin: " + user.username) 

 

    def test_get_bad_user(self): 

        """We shouldn't get a hit if the user is inactive""" 

        user = UserMgr.get(username='noexist') 

 

        eq_(user, None, 

            "Should not find a non-existant user: " + str(user))