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

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

"""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.bcelery import tasks 

from bookie.models import DBSession 

from bookie.tests import BOOKIE_TEST_INI 

from bookie.tests import empty_db 

 

GOOGLE_HASH = 'aa2239c17609b2' 

BMARKUS_HASH = 'c5c21717c99797' 

LOG = logging.getLogger(__name__) 

 

API_KEY = None 

 

 

class BookieAPITest(unittest.TestCase): 

    """Test the Bookie API""" 

 

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

 

        global API_KEY 

        res = DBSession.execute( 

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

        API_KEY = res['api_key'] 

 

    def tearDown(self): 

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

        testing.tearDown() 

        empty_db() 

 

    def _get_good_request(self, content=False, second_bmark=False): 

        """Return the basics for a good add bookmark request""" 

        session = DBSession() 

 

        # the main bookmark, added second to prove popular will sort correctly 

        prms = { 

            'url': u'http://google.com', 

            'description': u'This is my google desc', 

            'extended': u'And some extended notes about it in full form', 

            'tags': u'python search', 

            'api_key': API_KEY, 

            'username': 'admin', 

            'inserted_by': 'chrome_ext', 

        } 

 

        # if we want to test the readable fulltext side we want to make sure we 

        # pass content into the new bookmark 

        if content: 

            prms['content'] = "<p>There's some content in here dude</p>" 

 

        # req_params = urllib.urlencode(prms) 

        res = self.testapp.post( 

            '/api/v1/admin/bmark?', 

            content_type='application/json', 

            params=json.dumps(prms), 

        ) 

 

        if second_bmark: 

            prms = { 

                'url': u'http://bmark.us', 

                'description': u'Bookie', 

                'extended': u'Exteded notes', 

                'tags': u'bookmarks', 

                'api_key': API_KEY, 

                'username': 'admin', 

                'inserted_by': 'chrome_ext', 

            } 

 

            # if we want to test the readable fulltext side we want to make 

            # sure we pass content into the new bookmark 

            prms['content'] = "<h1>Second bookmark man</h1>" 

 

            # req_params = urllib.urlencode(prms) 

            res = self.testapp.post( 

                '/api/v1/admin/bmark?', 

                content_type='application/json', 

                params=json.dumps(prms) 

            ) 

 

        session.flush() 

        transaction.commit() 

        # Run the celery task for indexing this bookmark. 

        tasks.reindex_fulltext_allbookmarks(sync=True) 

        return res 

 

    def test_add_bookmark(self): 

        """We should be able to add a new bookmark to the system""" 

        # we need to know what the current admin's api key is so we can try to 

        # add 

        res = DBSession.execute( 

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

        key = res['api_key'] 

 

        test_bmark = { 

            'url': u'http://bmark.us', 

            'description': u'Bookie', 

            'extended': u'Extended notes', 

            'tags': u'bookmarks', 

            'api_key': key, 

        } 

 

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

                                params=test_bmark, 

                                status=200) 

 

        ok_('"location":' in res.body, 

            "Should have a location result: " + res.body) 

        ok_('description": "Bookie"' in res.body, 

            "Should have Bookie in description: " + res.body) 

 

    def test_bookmark_fetch(self): 

        """Test that we can get a bookmark and it's details""" 

        self._get_good_request(content=True) 

        res = self.testapp.get('/api/v1/admin/bmark/{0}?api_key={1}'.format( 

                               GOOGLE_HASH, 

                               API_KEY), 

                               status=200) 

 

        # make sure we can decode the body 

        bmark = json.loads(res.body)['bmark'] 

        eq_(GOOGLE_HASH, bmark[u'hash_id'], 

            "The hash_id should match: " + str(bmark[u'hash_id'])) 

 

        ok_(u'tags' in bmark, 

            "We should have a list of tags in the bmark returned") 

 

        ok_(bmark[u'tags'][0][u'name'] in [u'python', u'search'], 

            "Tag should be either python or search:" + 

            str(bmark[u'tags'][0][u'name'])) 

 

        ok_(u'readable' not in bmark, 

            "We should not have readable content") 

 

        eq_(u'python search', bmark[u'tag_str'], 

            "tag_str should be populated: " + str(dict(bmark))) 

 

        # to get readble content we need to pass the flash with_content 

        res = self.testapp.get( 

            '/api/v1/admin/bmark/{0}?api_key={1}&with_content=true'.format( 

            GOOGLE_HASH, 

            API_KEY), 

            status=200) 

 

        # make sure we can decode the body 

        bmark = json.loads(res.body)['bmark'] 

 

        ok_(u'readable' in bmark, 

            "We should have readable content") 

 

        ok_('dude' in bmark['readable']['content'], 

            "We should have 'dude' in our content: " + 

            bmark['readable']['content']) 

 

    def test_bookmark_fetch_fail(self): 

        """Verify we get a failed response when wrong bookmark""" 

        self._get_good_request() 

 

        # test that we get a 404 

        self.testapp.get( 

            '/api/v1/admin/bmark/{0}?api_key={1}'.format(BMARKUS_HASH, 

                                                         API_KEY), 

            status=404) 

 

    def test_bookmark_remove(self): 

        """A delete call should remove the bookmark from the system""" 

        self._get_good_request(content=True, second_bmark=True) 

 

        # now let's delete the google bookmark 

        res = self.testapp.delete( 

            '/api/v1/admin/bmark/{0}?api_key={1}'.format( 

                GOOGLE_HASH, 

                API_KEY), 

            status=200) 

 

        ok_('message": "done"' in res.body, 

            "Should have a message of done: " + res.body) 

 

        # we're going to cheat like mad, use the sync call to get the hash_ids 

        # of bookmarks in the system and verify that only the bmark.us hash_id 

        # is in the response body 

        res = self.testapp.get('/api/v1/admin/extension/sync', 

                               params={'api_key': API_KEY}, 

                               status=200) 

 

        ok_(GOOGLE_HASH not in res.body, 

            "Should not have the google hash: " + res.body) 

        ok_(BMARKUS_HASH in res.body, 

            "Should have the bmark.us hash: " + res.body) 

 

    def test_bookmark_recent_user(self): 

        """Test that we can get list of bookmarks with details""" 

        self._get_good_request(content=True) 

        res = self.testapp.get('/api/v1/admin/bmarks?api_key=' + API_KEY, 

                               status=200) 

 

        # make sure we can decode the body 

        bmark = json.loads(res.body)['bmarks'][0] 

        eq_(GOOGLE_HASH, bmark[u'hash_id'], 

            "The hash_id should match: " + str(bmark[u'hash_id'])) 

 

        ok_(u'tags' in bmark, 

            "We should have a list of tags in the bmark returned") 

 

        ok_(bmark[u'tags'][0][u'name'] in [u'python', u'search'], 

            "Tag should be either python or search:" + 

            str(bmark[u'tags'][0][u'name'])) 

 

        res = self.testapp.get( 

            '/api/v1/admin/bmarks?with_content=true&api_key=' + API_KEY, 

            status=200) 

 

        # make sure we can decode the body 

        # @todo this is out because of the issue noted in the code. We'll 

        # clean this up at some point. 

        # bmark = json.loads(res.body)['bmarks'][0] 

        # ok_('here dude' in bmark[u'readable']['content'], 

        #     "There should be content: " + str(bmark)) 

 

    def test_bookmark_recent(self): 

        """Test that we can get list of bookmarks with details""" 

        self._get_good_request(content=True) 

        res = self.testapp.get('/api/v1/bmarks?api_key=' + API_KEY, 

                               status=200) 

 

        # make sure we can decode the body 

        bmark = json.loads(res.body)['bmarks'][0] 

        eq_(GOOGLE_HASH, bmark[u'hash_id'], 

            "The hash_id should match: " + str(bmark[u'hash_id'])) 

 

        ok_(u'tags' in bmark, 

            "We should have a list of tags in the bmark returned") 

 

        ok_(bmark[u'tags'][0][u'name'] in [u'python', u'search'], 

            "Tag should be either python or search:" + 

            str(bmark[u'tags'][0][u'name'])) 

 

        res = self.testapp.get( 

            '/api/v1/admin/bmarks?with_content=true&api_key=' + API_KEY, 

            status=200) 

 

        # make sure we can decode the body 

        # @todo this is out because of the issue noted in the code. We'll 

        # clean this up at some point. 

        # bmark = json.loads(res.body)['bmarks'][0] 

        # ok_('here dude' in bmark[u'readable']['content'], 

        #     "There should be content: " + str(bmark)) 

 

    def test_bookmark_sync(self): 

        """Test that we can get the sync list from the server""" 

        self._get_good_request(content=True, second_bmark=True) 

 

        # test that we only get one resultback 

        res = self.testapp.get('/api/v1/admin/extension/sync', 

                               params={'api_key': API_KEY}, 

                               status=200) 

 

        eq_(res.status, "200 OK", 

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

 

        ok_(GOOGLE_HASH in res.body, 

            "The google hash id should be in the json: " + res.body) 

        ok_(BMARKUS_HASH in res.body, 

            "The bmark.us hash id should be in the json: " + res.body) 

 

    def test_search_api(self): 

        """Test that we can get list of bookmarks ordered by clicks""" 

        self._get_good_request(content=True, second_bmark=True) 

 

        res = self.testapp.get('/api/v1/bmarks/search/google', status=200) 

 

        # make sure we can decode the body 

        bmark_list = json.loads(res.body) 

        results = bmark_list['search_results'] 

        eq_(len(results), 1, 

            "We should have one result coming back: {0}".format(len(results))) 

 

        bmark = results[0] 

 

        eq_(GOOGLE_HASH, bmark[u'hash_id'], 

            "The hash_id {0} should match: {1} ".format( 

                str(GOOGLE_HASH), 

                str(bmark[u'hash_id']))) 

 

        ok_('clicks' in bmark, 

            "The clicks field should be in there") 

 

    def test_bookmark_tag_complete(self): 

        """Test we can complete tags in the system 

 

        By default we should have tags for python, search, bookmarks 

 

        """ 

        self._get_good_request(second_bmark=True) 

 

        res = self.testapp.get( 

            '/api/v1/admin/tags/complete', 

            params={ 

                'tag': 'py', 

                'api_key': API_KEY}, 

            status=200) 

 

        ok_('python' in res.body, 

            "Should have python as a tag completion: " + res.body) 

 

        # we shouldn't get python as an option if we supply bookmarks as the 

        # current tag. No bookmarks have both bookmarks & python as tags 

        res = self.testapp.get( 

            '/api/v1/admin/tags/complete', 

            params={ 

                'tag': 'py', 

                'current': 'bookmarks', 

                'api_key': API_KEY 

            }, 

            status=200) 

 

        ok_('python' not in res.body, 

            "Should not have python as a tag completion: " + res.body) 

 

    def test_account_information(self): 

        """Test getting a user's account information""" 

        res = self.testapp.get('/api/v1/admin/account?api_key=' + API_KEY, 

                               status=200) 

 

        # make sure we can decode the body 

        user = json.loads(res.body) 

 

        eq_(user['username'], 'admin', 

            "Should have a username of admin {0}".format(user)) 

 

        ok_('password' not in user, 

            'Should not have a field password {0}'.format(user)) 

        ok_('_password' not in user, 

            'Should not have a field password {0}'.format(user)) 

        ok_('api_key' not in user, 

            'Should not have a field password {0}'.format(user)) 

 

    def test_account_update(self): 

        """Test updating a user's account information""" 

        params = { 

            'name': u'Test Admin' 

        } 

        res = self.testapp.post( 

            str("/api/v1/admin/account?api_key=" + str(API_KEY)), 

            content_type='application/json', 

            params=json.dumps(params), 

            status=200) 

 

        # make sure we can decode the body 

        user = json.loads(res.body) 

 

        eq_(user['username'], 'admin', 

            "Should have a username of admin {0}".format(user)) 

        eq_(user['name'], 'Test Admin', 

            "Should have a new name of Test Admin {0}".format(user)) 

 

        ok_('password' not in user, 

            "Should not have a field password {0}".format(user)) 

        ok_('_password' not in user, 

            "Should not have a field password {0}".format(user)) 

        ok_('api_key' not in user, 

            "Should not have a field password {0}".format(user)) 

 

    def test_account_apikey(self): 

        """Fetching a user's api key""" 

        res = self.testapp.get("/api/v1/admin/api_key?api_key=" + str(API_KEY), 

                               status=200) 

 

        # make sure we can decode the body 

        user = json.loads(res.body) 

 

        eq_(user['username'], 'admin', 

            "Should have a username of admin {0}".format(user)) 

        ok_('api_key' in user, 

            "Should have an api key in there: {0}".format(user)) 

 

    def test_account_password_change(self): 

        """Change a user's password""" 

        params = { 

            'current_password': 'admin', 

            'new_password': 'not_testing' 

        } 

 

        res = self.testapp.post( 

            "/api/v1/admin/password?api_key=" + str(API_KEY), 

            params=params, 

            status=200) 

 

        # make sure we can decode the body 

        user = json.loads(res.body) 

 

        eq_(user['username'], 'admin', 

            "Should have a username of admin {0}".format(user)) 

        ok_('message' in user, 

            "Should have a message key in there: {0}".format(user)) 

 

        params = { 

            'current_password': 'not_testing', 

            'new_password': 'admin' 

        } 

        res = self.testapp.post( 

            "/api/v1/admin/password?api_key=" + str(API_KEY), 

            params=params, 

            status=200) 

 

    def test_account_password_failure(self): 

        """Change a user's password, in bad ways""" 

        params = { 

            'current_password': 'test', 

            'new_password': 'not_testing' 

        } 

 

        res = self.testapp.post( 

            "/api/v1/admin/password?api_key=" + str(API_KEY), 

            params=params, 

            status=403) 

 

        # make sure we can decode the body 

        user = json.loads(res.body) 

 

        eq_(user['username'], 'admin', 

            "Should have a username of admin {0}".format(user)) 

        ok_('error' in user, 

            "Should have a error key in there: {0}".format(user)) 

        ok_('typo' in user['error'], 

            "Should have a error key in there: {0}".format(user)) 

 

    def test_api_ping_success(self): 

        """We should be able to ping and make sure we auth'd and are ok""" 

        res = self.testapp.get('/api/v1/admin/ping?api_key=' + API_KEY, 

                               status=200) 

        ping = json.loads(res.body) 

 

        ok_(ping['success'], 

            "Should success be true") 

 

    def test_api_ping_failed_nouser(self): 

        """If you don't supply a username, you've failed the ping""" 

        res = self.testapp.get('/api/v1/ping?api_key=' + API_KEY, 

                               status=200) 

        ping = json.loads(res.body) 

 

        ok_(not ping['success'], 

            "Success should be false") 

        eq_(ping['message'], "Missing username in your api url.") 

 

    def test_api_ping_failed_missing_api(self): 

        """If you don't supply a username, you've failed the ping""" 

        res = self.testapp.get('/ping?api_key=' + API_KEY, 

                               status=200) 

        ping = json.loads(res.body) 

 

        ok_(not ping['success'], 

            "Success should be false") 

        eq_(ping['message'], "The API url should be /api/v1")