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

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

"""Test that we're meeting delicious API specifications""" 

import logging 

import json 

import transaction 

import unittest 

from nose.tools import ok_, eq_ 

from pyramid import testing 

 

from bookie.models import Bmark 

from bookie.models import DBSession 

from bookie.models.auth import Activation 

from bookie.models.queue import ImportQueue 

from bookie.tests import BOOKIE_TEST_INI 

from bookie.tests import empty_db 

from bookie.tests import factory 

 

LOG = logging.getLogger(__name__) 

 

 

class AdminApiTest(unittest.TestCase): 

    """Test the bookie admin api calls.""" 

    _api_key = None 

 

    @property 

    def api_key(self): 

        """Cache the api key for all calls.""" 

        if not self._api_key: 

            res = DBSession.execute( 

                "SELECT api_key FROM users WHERE username='admin'").fetchone() 

            self._api_key = res['api_key'] 

        return self._api_key 

 

    def setUp(self): 

        from pyramid.paster import get_app 

        app = get_app(BOOKIE_TEST_INI, 'bookie') 

        from webtest import TestApp 

        self.testapp = TestApp(app) 

        testing.setUp() 

 

    def tearDown(self): 

        """We need to empty the bmarks table on each run""" 

        testing.tearDown() 

        empty_db() 

 

    def _add_demo_import(self): 

        """DB Needs some imports to be able to query.""" 

        # add out completed one 

        q = ImportQueue( 

            username='admin', 

            file_path='testing.txt' 

        ) 

        DBSession.add(q) 

        transaction.commit() 

        return 

 

    def test_list_inactive_users(self): 

        """Test that we can fetch the inactive users.""" 

        # for now just make sure we can get a 200 call on it. 

        params = { 

            'api_key': self.api_key 

        } 

        res = self.testapp.get('/api/v1/a/accounts/inactive', 

                               params=params, 

                               status=200) 

        # by default we shouldn't have any inactive users 

        data = json.loads(res.body) 

        users = [u for u in data['users']] 

        for u in users: 

            eq_(0, u['invite_ct'], "Count should be 0 to start.") 

 

    def test_invite_ct(self): 

        """Test we can call and get the invite counts.""" 

        # for now just make sure we can get a 200 call on it. 

        params = { 

            'api_key': self.api_key 

        } 

        res = self.testapp.get('/api/v1/a/accounts/invites', 

                               params=params, 

                               status=200) 

        # we should get back tuples of username/count 

        data = json.loads(res.body)['users'] 

        found = False 

        invite_count = None 

        for user, count in data: 

            if user == u'admin': 

                found = True 

                invite_count = count 

 

        ok_(found, "There should be the admin user." + res.body) 

        eq_(0, invite_count, 

            "The admin user shouldn't have any invites." + res.body) 

 

    def test_set_invite_ct(self): 

        """Test we can set the invite count for the user""" 

        # for now just make sure we can get a 200 call on it. 

        params = { 

            'api_key': self.api_key 

        } 

        res = self.testapp.post('/api/v1/a/accounts/invites/admin/10', 

                                params=params, 

                                status=200) 

        # we should get back tuples of username/count 

        data = json.loads(res.body) 

        eq_('admin', data.get('username'), 

            "The admin user data is returned to us." + res.body) 

        eq_(10, int(data.get('invite_ct')), 

            "The admin user now has 10 invites." + res.body) 

 

        # and of course when we're done we need to unset it back to 0 or else 

        # the test above blows up...sigh. 

        res = self.testapp.post('/api/v1/a/accounts/invites/admin/0', 

                                params=params, 

                                status=200) 

 

    def test_import_info(self): 

        """Test that we can get a count of the imports in the system.""" 

        self._add_demo_import() 

        params = { 

            'api_key': self.api_key 

        } 

        res = self.testapp.get('/api/v1/a/imports/list', 

                               params=params, 

                               status=200) 

 

        # we should get back tuples of username/count 

        data = json.loads(res.body) 

 

        eq_(1, data.get('count'), "There are none by default. " + res.body) 

 

        eq_('admin', data.get('imports')[0]['username'], 

            "The first import is from admin " + res.body) 

        eq_(0, data.get('imports')[0]['status'], 

            "And it has a status of 0 " + res.body) 

 

    def test_user_list(self): 

        """Test that we can hit the api and get the list of users.""" 

        self._add_demo_import() 

        params = { 

            'api_key': self.api_key 

        } 

        res = self.testapp.get('/api/v1/a/users/list', 

                               params=params, 

                               status=200) 

 

        # we should get back dict of count, users. 

        data = json.loads(res.body) 

 

        eq_(1, data.get('count'), "There are none by default. " + res.body) 

        eq_('admin', data.get('users')[0]['username'], 

            "The first user is from admin " + res.body) 

        eq_('testing@dummy.com', data.get('users')[0]['email'], 

            "The first user is from testing@dummy.com " + res.body) 

 

    def test_user_delete(self): 

        """Verify we can remove a user and their bookmarks via api.""" 

        bob = factory.make_user(username='bob') 

        bob.activation = Activation('signup') 

 

        factory.make_bookmark(user=bob) 

        transaction.commit() 

 

        res = self.testapp.delete( 

            '/api/v1/a/users/delete/{0}?api_key={1}'.format( 

                'bob', 

                self.api_key), 

            status=200) 

 

        # we should get back dict of count, users. 

        data = json.loads(res.body) 

 

        ok_(data.get('success')) 

 

        # Verify that we have no bookmark for the user any longer. 

        bmarks = Bmark.query.filter(Bmark.username == 'bob').all() 

        eq_(0, len(bmarks))