Пример #1
0
    def test_delete_db_ndb_mixed(self):
        # Start empty
        storage_ndb = StorageByKeyName(
            CredentialsNDBModel, 'foo', 'credentials')
        storage = StorageByKeyName(
            CredentialsModel, 'foo', 'credentials')

        # First DB, then NDB
        self.assertEqual(None, storage.get())
        storage.put(self.credentials)
        self.assertNotEqual(None, storage.get())

        storage_ndb.delete()
        self.assertEqual(None, storage.get())

        # First NDB, then DB
        self.assertEqual(None, storage_ndb.get())
        storage_ndb.put(self.credentials)

        storage.delete()
        self.assertNotEqual(None, storage_ndb.get())
        # NDB uses memcache and an instance cache (Context)
        ndb.get_context().clear_cache()
        memcache.flush_all()
        self.assertEqual(None, storage_ndb.get())
Пример #2
0
    def get_token(self):
        secrets_filepath = os.path.join(os.path.dirname(__file__),
                                        'secrets/urltoken.json')
        with open(secrets_filepath, 'r') as fp:
            secrets = json.load(fp)
        token = hashlib.sha256(self.user_id.encode() +
                               secrets.get('hash_seed').encode()).hexdigest()

        if (self.credentials.refresh_token):
            storage = StorageByKeyName(TokenizedCredentialsModel, token,
                                       'credentials')
            storage.put(self.credentials)

        return token
Пример #3
0
    def test_get_and_put_set_store_on_cache_retrieval(self):
        storage = StorageByKeyName(
            CredentialsModel, 'foo', 'credentials', cache=memcache)

        self.assertEqual(None, storage.get())
        self.credentials.set_store(storage)
        storage.put(self.credentials)
        # Pre-bug 292 old_creds wouldn't have storage, and the _refresh
        # wouldn't be able to store the updated cred back into the storage.
        old_creds = storage.get()
        self.assertEqual(old_creds.access_token, 'foo')
        old_creds.invalid = True
        old_creds._refresh(_http_request)
        new_creds = storage.get()
        self.assertEqual(new_creds.access_token, 'bar')
Пример #4
0
    def test_delete_ndb(self):
        # Start empty
        storage = StorageByKeyName(CredentialsNDBModel, 'foo', 'credentials')
        self.assertEqual(None, storage.get())

        # Add credentials to model with storage, and check equivalent
        # w/o storage
        storage.put(self.credentials)
        credmodel = CredentialsNDBModel.get_by_id('foo')
        self.assertEqual(credmodel.credentials.to_json(),
                         self.credentials.to_json())

        # Delete and make sure empty
        storage.delete()
        self.assertEqual(None, storage.get())
Пример #5
0
    def test_delete_ndb(self):
        # Start empty
        storage = StorageByKeyName(
            CredentialsNDBModel, 'foo', 'credentials')
        self.assertEqual(None, storage.get())

        # Add credentials to model with storage, and check equivalent
        # w/o storage
        storage.put(self.credentials)
        credmodel = CredentialsNDBModel.get_by_id('foo')
        self.assertEqual(credmodel.credentials.to_json(),
                         self.credentials.to_json())

        # Delete and make sure empty
        storage.delete()
        self.assertEqual(None, storage.get())
Пример #6
0
    def test_get_and_put_set_store_on_cache_retrieval(self):
        storage = StorageByKeyName(CredentialsModel,
                                   'foo',
                                   'credentials',
                                   cache=memcache)

        self.assertEqual(None, storage.get())
        self.credentials.set_store(storage)
        storage.put(self.credentials)
        # Pre-bug 292 old_creds wouldn't have storage, and the _refresh
        # wouldn't be able to store the updated cred back into the storage.
        old_creds = storage.get()
        self.assertEqual(old_creds.access_token, 'foo')
        old_creds.invalid = True
        old_creds._refresh(_http_request)
        new_creds = storage.get()
        self.assertEqual(new_creds.access_token, 'bar')
Пример #7
0
    def get(self):
        """Handler for GET requests to the app."""
        http = OAUTH_DECORATOR.http()
        if not self.request.get('videoId'):
            self.response.write(
                """Please supply the video ID of a YouTube live stream as a query
                                parameter, i.e. ?videoId=xxx.""")
            return

        youtube = build('youtube', 'v3', http=http)
        channel = youtube.channels().list(mine=True, part='id').execute()
        channel_id = channel['items'][0]['id']

        storage = StorageByKeyName(CredentialsModel, channel_id, 'credentials')
        storage.put(OAUTH_DECORATOR.credentials)

        video_id = self.request.get('videoId')
        videos = youtube.videos().list(id=video_id,
                                       part="liveStreamingDetails").execute()
        video = videos['items'][0]
        live_chat_id = video['liveStreamingDetails']['activeLiveChatId']

        # See if the bot's already in the channel.
        in_chat = memcache.get("{}:in_chat".format(live_chat_id))
        if in_chat:
            self.response.write(
                """The bot's already in that chat! Try saying .hi to
             it, or asking it to .leave! If the bot isn't in chat, wait 4 minutes
            then try adding it again""")
        else:
            taskqueue.add(url='/spawnbot',
                          target='worker',
                          params={
                              'channel_id': channel_id,
                              'live_chat_id': live_chat_id
                          })

            self.response.write(
                "Created the bot task for live chat " + live_chat_id +
                " on channel " + channel_id +
                "! The bot should join the channel soon and say hello :)")
Пример #8
0
    def test_delete_db_ndb_mixed(self):
        # Start empty
        storage_ndb = StorageByKeyName(CredentialsNDBModel, 'foo',
                                       'credentials')
        storage = StorageByKeyName(CredentialsModel, 'foo', 'credentials')

        # First DB, then NDB
        self.assertEqual(None, storage.get())
        storage.put(self.credentials)
        self.assertNotEqual(None, storage.get())

        storage_ndb.delete()
        self.assertEqual(None, storage.get())

        # First NDB, then DB
        self.assertEqual(None, storage_ndb.get())
        storage_ndb.put(self.credentials)

        storage.delete()
        self.assertNotEqual(None, storage_ndb.get())
        # NDB uses memcache and an instance cache (Context)
        ndb.get_context().clear_cache()
        memcache.flush_all()
        self.assertEqual(None, storage_ndb.get())