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

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

"""Test the password reset step process 

 

 

- You've forgotten your password 

- You enter your email into the forgotten password ui 

    - Your account gets a activation record 

    - Your account is deactivated 

    - An email with the activation url is emailed to you 

- You cannot re-enter the account for activation until the previous one is 

  expired/or a successful reset has occurred 

- While the account is deactivated you cannot make api calls or view login-only 

  urls 

- You follow the activation link and can reset your password 

- At this point you can log in with the new password 

- api and other calls now function 

 

""" 

import json 

import logging 

import transaction 

 

from nose.tools import ok_ 

from pyramid import testing 

from unittest import TestCase 

 

from bookie.models import DBSession 

from bookie.models.auth import Activation 

 

LOG = logging.getLogger(__name__) 

 

 

class TestReactivateFunctional(TestCase): 

 

    def _reset_admin(self): 

        """Reset the admin account""" 

        DBSession.execute( 

            "UPDATE users SET activated='1' WHERE username='admin';") 

        Activation.query.delete() 

        transaction.commit() 

 

    def setUp(self): 

        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): 

        self._reset_admin() 

        testing.tearDown() 

 

    def test_activate_form_bad(self): 

        """Test bad call to reset""" 

        res = self.testapp.post( 

            '/api/v1/suspend', 

            content_type='application/json', 

            status=406) 

        success = json.loads(res.body)['error'] 

        ok_(success is not None, 

            "Should not be successful with no email address: " + str(res)) 

 

        res = self.testapp.post('/api/v1/suspend', 

                                params={'email': 'notexist@gmail.com'}, 

                                status=404) 

        success = json.loads(res.body) 

        ok_('error' in success, 

            "Should not be successful with invalid email address: " + str(res)) 

 

    def test_activate_form(self): 

        """ Functional test to see if we can submit the api to reset an account 

 

        Now by doing this we end up marking the account deactivated which 

        causes other tests to 403 it up. Need to reinstate the admin account on 

        tearDown 

 

        """ 

        res = self.testapp.post('/api/v1/suspend', 

                                params={'email': u'testing@dummy.com'}, 

                                status=200) 

 

        success = json.loads(res.body) 

        ok_('message' in success, 

            "Should be successful with admin email address: " + str(res)) 

 

    def test_activate_form_dual(self): 

        """Test that we can't resubmit for reset, get prompted to email 

 

        If we reset and then try to say "I've forgotten" a second time, we 

        should get a nice message. And that message should allow us to get a 

        second copy of the email sent. 

 

        """ 

        res = self.testapp.post('/api/v1/suspend', 

                                params={'email': u'testing@dummy.com'}, 

                                status=200) 

 

        success = json.loads(res.body) 

        ok_('message' in success, 

            "Should be successful with admin email address") 

 

        res = self.testapp.post('/api/v1/suspend', 

                                params={'email': u'testing@dummy.com'}, 

                                status=406) 

 

        success = json.loads(res.body) 

        ok_('error' in success, 

            "Should not be successful on second try: " + str(res)) 

 

        ok_('already' in str(res), 

            "Should find 'already' in the response: " + str(res)) 

 

    def test_reactivate_process(self): 

        """Walk through all of the steps at a time 

 

        - First we mark that we've forgotten 

        - Then use make sure we get a 403 accessing something 

        - Then we go back through our activation using our code 

        - Finally verify we can access the earlier item 

 

        """ 

        res = self.testapp.post('/api/v1/suspend', 

                                params={'email': u'testing@dummy.com'}, 

                                status=200) 

 

        success = json.loads(res.body) 

        ok_('message' in success, 

            "Should be successful with admin email address") 

 

        # now let's try to login 

        # the migrations add a default admin account 

        user_data = {'login': 'admin', 

                     'password': 'admin', 

                     'form.submitted': 'true'} 

 

        res = self.testapp.post('/login', 

                                params=user_data, 

                                status=200) 

 

        ok_('account deactivated' in str(res), 

            "Login should have failed since we're not active: " + str(res)) 

 

        act = Activation.query.first() 

        self.testapp.delete( 

            "/api/v1/suspend?username={0}&code={1}&password={2}".format( 

                user_data['login'], 

                act.code, 

                'admin'), 

            status=200) 

 

        ok_('activated' in str(res), 

            "Should be prompted to login now: " + str(res)) 

 

        user_data = {'login': 'admin', 

                     'password': 'admin', 

                     'form.submitted': 'true'} 

 

        res = self.testapp.post('/login', 

                                params=user_data, 

                                status=302)