Source code for test.server

#!/usr/bin/env python

import os
import unittest
from getpass import getpass

from skpy import Skype, SkypeNewMessageEvent

# Slightly less verbose access to environment variables.
env = dict((x, os.getenv("SKPY_TESTSERVER_{0}".format(x.upper()))) for x in ("tokens", "recip"))

[docs]class SkypeServerTestBase(unittest.TestCase): """ Base class for tests designed to interpret server responses. .. warning:: This requires access to a live Skype account, the credentials for which will be read from **.tokens** (or from a file named in environment variable ``SKPY_TESTSERVER_TOKENS`` if set). This can be accomplished in a shell:: >>> from skpy import Skype >>> Skype(username, password, tokenFile=".tokens") You must also set ``SKPY_TESTSERVER_RECIP`` to a contact on the account that should receive test messages. """ @classmethod def setUpClass(cls): = Skype(tokenFile=env["tokens"] or ".tokens") if not raise RuntimeError("Token file is invalid") cls.recip = env["recip"] if not cls.recip: raise RuntimeError("No recipient specified (SKPY_TESTSERVER_RECIP)")
[docs]class SkypeServerReadTest(SkypeServerTestBase): """ Basic, read-only tests on information provided by the server for the connected account. """
[docs] def testSelf(self): """ Retrieve the current user. """ self.assertTrue( ==, "Wrong user identifier")
[docs] def testSettings(self): """ Read all setting fields for the current user. """ for setting in getattr(, setting)
[docs] def testContacts(self): """ Retrieve the named recipient as a user and a contact. """ self.assertTrue( == self.recip, "Failed to lookup user") self.assertTrue(self.recip in ( for contact in, "No contacts returned") self.assertTrue([self.recip].id == self.recip, "Failed to lookup cached contact") self.assertTrue( == self.recip, "Failed to lookup full contact") self.assertTrue("concierge").id == "concierge") self.assertTrue(
[docs] def testChats(self): """ Retrieve a conversation with the named recipient. """ chatId = "8:{0}".format(self.recip) chat =[chatId] self.assertTrue( == chatId, "Wrong chat: {0}".format( self.assertTrue(chat.userId == self.recip, "Wrong recipient: {0}".format(chat.userId))
[docs] def testTranslate(self): """ Request a text translation. """ self.assertTrue("en" in"Skype server test", "fr"), "en", "fr")
[docs] def testServices(self): """ Retrieve the services list for the current user. """ self.assertTrue(
[docs]class SkypeServerWriteTest(SkypeServerTestBase): """ Specific test cases that require performing "write" actions on the connected account. """
[docs] def testGroupChats(self): """ Create a group chat with the named recipient, send a test message, and invite a guest. """ chat =[self.recip]) try: chat.setTopic("Skype server test") self.assertTrue(set(chat.userIds) == set([, self.recip]), "Wrong group recipients: {0}".format(", ".join(chat.userIds))) msg = chat.sendMsg("Test message.") self.assertTrue(msg.chatId ==, "Wrong group chat: {0}".format( self.assertTrue(msg.content == "Test message.", "Wrong message: {0}".format(msg.content)) chat.setHistory(False) chat.setOpen(True) skGuest = Skype() skGuest.conn.guestLogin(chat.joinUrl, "Test") chatGuest = skGuest.chats[] try: msgGuest = chatGuest.sendMsg("Test message from guest.") self.assertTrue(msgGuest.chatId ==, "Wrong guest group chat: {0}".format( self.assertTrue(msgGuest.content == "Test message from guest.", "Wrong guest message: {0}".format(msg.content)) finally: chatGuest.leave() chat.setOpen(False) finally: chat.leave() chat.delete()
[docs]class SkypeServerEventTest(SkypeServerTestBase): """ Specific test cases that poll the event stream, and require external data (i.e. sending messages from another client to this user account). """ @staticmethod def input(prompt): try: return raw_input(prompt) except NameError: return input(prompt)
[docs] def testPasswordLogin(self): """ Attempt a fresh login with a username and password. """ user = if user.startswith("live:"): user = self.input("> Microsoft account email address: ") pwd = getpass("> Microsoft account password: ") else: pwd = getpass("> Skype account password: ") sk = Skype(user, pwd) self.assertTrue(sk.conn.connected)
[docs] def testMessageEvent(self): """ Receive a message from the named recipient. """ print("") print("> Send a message from {0} to {1} now.".format(, self.recip)) while True: dead = True for event in dead = False if isinstance(event, SkypeNewMessageEvent) and event.msg.chatId == "8:{0}".format(self.recip): return if dead:"No events received")
if __name__ == "__main__": unittest.main()