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

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

import feedparser 

import logging 

import time 

import transaction 

from datetime import datetime 

from nose.tools import ok_, eq_ 

 

from bookie.models import Bmark 

from bookie.tests import TestViewBase 

from bookie.tests.factory import make_bookmark 

 

LOG = logging.getLogger(__name__) 

 

 

class BookieViewsTest(TestViewBase): 

    """Test the normal web views user's user""" 

 

    def _add_bmark(self): 

        # setup the default bookie bookmark 

        import logging 

        log = logging.getLogger(__name__) 

        log.error('called to add bmark') 

        bmark_us = Bmark('http://bmark.us', 

                         username="admin", 

                         desc=u"Bookie Website", 

                         ext=u"Bookie Documentation Home", 

                         tags=u"bookmarks") 

 

        bmark_us.stored = datetime.now() 

        bmark_us.updated = datetime.now() 

        transaction.commit() 

 

    def test_bookmark_recent(self): 

        """Verify we can call the /recent url """ 

        self._add_bmark() 

        body_str = "Recent Bookmarks" 

 

        res = self.app.get('/recent') 

 

        eq_(res.status, "200 OK", 

            msg='recent status is 200, ' + res.status) 

        ok_(body_str in res.body, 

            msg="Request should contain body_str: " + res.body) 

 

    def test_recent_page(self): 

        """We should be able to page through the list""" 

        body_str = "Prev" 

 

        res = self.app.get('/recent?page=1') 

        eq_(res.status, "200 OK", 

            msg='recent page 1 status is 200, ' + res.status) 

        ok_(body_str in res.body, 

            msg="Page 1 should contain body_str: " + res.body) 

 

    def test_import_auth_failed(self): 

        """Veryify that without the right API key we get forbidden""" 

        post = { 

            'api_key': 'wrong_key' 

        } 

 

        res = self.app.post('/admin/import', params=post, status=403) 

 

        eq_(res.status, "403 Forbidden", 

            msg='Import status is 403, ' + res.status) 

 

    def test_changes_link_in_footer(self): 

        """Changes link should go to the bookie commits github page.""" 

        changes_link = "https://github.com/mitechie/Bookie/commits/develop" 

        res = self.app.get('/') 

 

        eq_(res.status, "200 OK", 

            msg='recent status is 200, ' + res.status) 

        ok_(changes_link in res.body, 

            msg="Changes link should appear: " + res.body) 

 

 

class TestNewBookmark(TestViewBase): 

    """Test the new bookmark real views""" 

 

    def test_renders(self): 

        """Verify that we can call the /new url""" 

        self._login_admin() 

        res = self.app.get('/admin/new') 

        ok_('Add Bookmark' in res.body, 

            "Should see the add bookmark title") 

 

    def test_manual_entry_error(self): 

        """Use can manually submit a bookmark.""" 

        self._login_admin() 

        # no url entered 

        res = self.app.post( 

            '/admin/new_error', 

            params={ 

                'url': '', 

                'description': '', 

                'extended': '', 

                'tags': '' 

            }) 

        self.assertIn('not valid', res.body) 

 

 

class TestRSSFeeds(TestViewBase): 

    """Verify the RSS feeds function correctly.""" 

 

    def test_rss_added(self): 

        """Viewing /recent should have a rss url in the content.""" 

        body_str = "application/rss+xml" 

        res = self.app.get('/recent') 

 

        eq_(res.status, "200 OK", 

            msg='recent status is 200, ' + res.status) 

        ok_(body_str in res.body, 

            msg="Request should contain rss str: " + res.body) 

 

    def test_rss_matches_request(self): 

        """The url should match the /recent request with tags.""" 

        body_str = "rss/ubuntu" 

        res = self.app.get('/recent/ubuntu') 

 

        eq_(res.status, "200 OK", 

            msg='recent status is 200, ' + res.status) 

        ok_(body_str in res.body, 

            msg="Request should contain rss url: " + res.body) 

 

    def test_rss_is_parseable(self): 

        """The rss feed should be a parseable feed.""" 

        [make_bookmark() for i in range(10)] 

        transaction.commit() 

 

        res = self.app.get('/rss') 

 

        eq_(res.status, "200 OK", 

            msg='recent status is 200, ' + res.status) 

 

        # http://packages.python.org/feedparser/ 

        # introduction.html#parsing-a-feed-from-a-string 

        parsed = feedparser.parse(res.body) 

        links = [] 

        for entry in parsed.entries: 

            links.append({ 

                'title': entry.title, 

                'category': entry.category, 

                'date': time.strftime('%d %b %Y', entry.updated_parsed), 

                'description': entry.description, 

                'link': entry.link, 

            }) 

 

        ok_(links, 'The feed should have a list of links.') 

        eq_(10, len(links), 'There are 10 links in the feed.') 

 

        sample_item = links[0] 

        ok_(sample_item['title'], 'Items have a title.') 

        ok_(sample_item['link'], 'Items have a link to reach things.') 

        ok_('description' in sample_item, 'Items have a description string.')