~alecu/ubuntuone-client/delivery-call-api

« back to all changes in this revision

Viewing changes to tests/syncdaemon/test_action_queue.py

  • Committer: Alejandro J. Cura
  • Date: 2013-01-25 14:55:12 UTC
  • Revision ID: alecu@canonical.com-20130125145512-1mq2skk5gnd1jwh0
Add call to newest-purchases web api

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#-*- coding: utf-8 -*-
2
2
#
3
 
# Copyright 2009-2012 Canonical Ltd.
 
3
# Copyright 2009-2013 Canonical Ltd.
4
4
#
5
5
# This program is free software: you can redistribute it and/or modify it
6
6
# under the terms of the GNU General Public License version 3, as published
487
487
        yield self.action_queue.get_webclient(self.fake_iri)
488
488
        self.assertEqual(calls[1]["context_factory"], fake_context)
489
489
 
 
490
    def test_queue_music_delivery_results(self):
 
491
        """The music delivery results are queued."""
 
492
        albums = ["fake", "list", "of", "albums"]
 
493
        self.action_queue.music_delivery_results(albums)
 
494
        expected = ('AQ_MUSIC_DELIVERY_RESULTS', {'purchased_albums': albums})
 
495
        self.assertEqual([expected], self.action_queue.event_queue.events)
 
496
 
 
497
 
 
498
class MusicDeliveryLifeCycleTestCase(BasicTestCase):
 
499
    """Tests to check the lifecycle of the music delivery checks."""
 
500
 
 
501
    def test_delivery_checker_initialization(self):
 
502
        """Check the initialization of the checker."""
 
503
        self.assertEqual(self.action_queue.delivery_checker.action_queue,
 
504
                         self.action_queue)
 
505
 
 
506
    def test_delivery_checker_started(self):
 
507
        """Check that the delivery checker is started on connection."""
 
508
        called = []
 
509
        self.patch(self.action_queue, "_lookup_srv", lambda: defer.Deferred())
 
510
        self.patch(self.action_queue.delivery_checker, "start",
 
511
            lambda: called.append(True))
 
512
        self.action_queue.connect()
 
513
        self.assertEqual(len(called), 1)
 
514
 
 
515
    def test_delivery_checker_stopped(self):
 
516
        """Check that the delivery checker is stopped on disconnection."""
 
517
        called = []
 
518
        self.patch(self.action_queue.delivery_checker, "stop",
 
519
            lambda: called.append(True))
 
520
        self.action_queue._cleanup_connection_state()
 
521
        self.assertEqual(len(called), 1)
 
522
 
 
523
 
 
524
FAKE_DELIVERY_RESPONSE = """{
 
525
  "server_timestamp": 1234,
 
526
  "purchased_albums": [{
 
527
    "album_name": "7digital Essential: 50s Rock",
 
528
    "cloud_delivery_status": "COMPLETED",
 
529
    "timestamp_purchased": 1230,
 
530
    "open_url": "http://one.ubuntu.com/......",
 
531
    "tracks_udf_paths": [
 
532
      "Bill Haley/7digital Essential: 50s Rock/Rock Around the Clock.mp3",
 
533
      "Bo Diddley/7digital Essential: 50s Rock/I'm a Man.mp3",
 
534
      "Buddy Holly/7digital Essential: 50s Rock/Little Baby.mp3",
 
535
      "Chuck Berry/7digital Essential: 50s Rock/Johnny Be Good.mp3",
 
536
      "Chuck Berry/7digital Essential: 50s Rock/Roll Over Beethoven.mp3",
 
537
      "Chuck Berry/7digital Essential: 50s Rock/Sweet Little Rock&Roll.mp3",
 
538
      "Eddie Cochran/7digital Essential: 50s Rock/Let's Get Together.mp3",
 
539
      "Eddie Cochran/7digital Essential: 50s Rock/My Way.mp3",
 
540
      "Elvis Presley/7digital Essential: 50s Rock/Blue Suede Shoes.mp3",
 
541
      "Elvis Presley/7digital Essential: 50s Rock/Don't Be Cruel.mp3",
 
542
      "Elvis Presley/7digital Essential: 50s Rock/Shake, Rattle and Roll.mp3"
 
543
    ]}
 
544
  ]
 
545
}"""
 
546
 
 
547
 
 
548
class FakeAQ(object):
 
549
    """A fake action queue."""
 
550
 
 
551
    def __init__(self, result=FAKE_DELIVERY_RESPONSE):
 
552
        self.result = result
 
553
        self.called = []
 
554
        self.delivery_results = None
 
555
 
 
556
    def webcall(self, *args, **kwargs):
 
557
        """A fake web call."""
 
558
        self.called.append((args, kwargs))
 
559
        response = action_queue.txweb.Response(self.result)
 
560
        return defer.succeed(response)
 
561
 
 
562
    def music_delivery_results(self, albums):
 
563
        """The music results are sent to the event queue."""
 
564
        self.delivery_results = albums
 
565
 
 
566
 
 
567
class MusicDeliveryCheckerTestCase(TwistedTestCase):
 
568
    """Tests for the MusicDeliveryChecker."""
 
569
 
 
570
    @defer.inlineCallbacks
 
571
    def test_webservice_called_with_timestamp(self):
 
572
        """The webservice is called with the last server timestamp."""
 
573
        fakeaq = FakeAQ()
 
574
        checker = action_queue.MusicDeliveryChecker(fakeaq)
 
575
        expected = ((action_queue.MusicDeliveryChecker.ALBUM_DELIVERY_IRI %
 
576
            checker.last_server_timestamp,), {'method': 'GET'})
 
577
        yield checker.check_api()
 
578
        self.assertEqual(fakeaq.called, [expected])
 
579
 
 
580
    @defer.inlineCallbacks
 
581
    def test_new_server_timestamp_is_stored(self):
 
582
        """The webservice response is json parsed and the timestamp stored."""
 
583
        fakeaq = FakeAQ()
 
584
        checker = action_queue.MusicDeliveryChecker(fakeaq)
 
585
        yield checker.check_api()
 
586
        self.assertEqual(checker.last_server_timestamp, 1234)
 
587
 
 
588
    @defer.inlineCallbacks
 
589
    def test_event_queue_is_pushed(self):
 
590
        """The webservice response is pushed into the event queue."""
 
591
        fakeaq = FakeAQ()
 
592
        checker = action_queue.MusicDeliveryChecker(fakeaq)
 
593
        yield checker.check_api()
 
594
        self.assertEqual(len(fakeaq.delivery_results), 1)
 
595
 
490
596
 
491
597
class TestLoggingStorageClient(TwistedTestCase):
492
598
    """Tests for ensuring magic hash dont show in logs."""