Exemplo n.º 1
0
    def setup_prerequisite(self):
        DatabaseManager.reset()

        self.bot = Bot(name=u'test', description=u'test',
                       interaction_timeout=120, session_timeout=86400).add()
        DatabaseManager.commit()

        config = {
            'access_token': 'EAAP0okfsZCVkBAI3BCU5s3u8O0iVFh6NAwFHa7X2bKZCGQ'
                            'Lw6VYeTpeTsW5WODeDbekU3ZA0JyVCBSmXq8EqwL1GDuZBO'
                            '7aAlcNEHQ3AZBIx0ZBfFLh95TlJWlLrYetzm9owKNR8Qju8'
                            'HF6qra20ZC6HqNXwGpaP74knlNvQJqUmwZDZD'
        }
        platform = Platform(name=u'Test Platform',
                            bot_id=self.bot.id,
                            type_enum=PlatformTypeEnum.Facebook,
                            provider_ident='1155924351125985',
                            config=config).add()
        DatabaseManager.commit()

        self.user = User(platform_id=platform.id,
                         platform_user_ident='1153206858057166',
                         last_seen=datetime.datetime.now()).add()

        DatabaseManager.commit()
Exemplo n.º 2
0
    def setup_prerequisite(self):
        DatabaseManager.reset()

        self.bot = Bot(name=u'test',
                       description=u'test',
                       interaction_timeout=120,
                       session_timeout=86400).add()
        DatabaseManager.commit()

        config = {
            'access_token':
            'iHRMgmp3zRLOc6kPCbPNMwEDHyFqLGSy0tyG3uZxnkNlhMKg'
            '8GVFqMGslcOkmgOAFLlBvvYuXmKF9odhXtsCm3tBxRcPryKr'
            'kOvzHBcBvS2zrVGiVmZGh5EBcqazgurYMwVSdgNSrhCm/qp6'
            '2aR7HAdB04t89/1O/w1cDnyilFU=',
            'channel_secret':
            '335c901df3a1969ca28a48bf6ddcc333'
        }
        platform = Platform(bot_id=self.bot.id,
                            name=u'Line',
                            type_enum=PlatformTypeEnum.Line,
                            provider_ident='aitjcize.line',
                            config=config).add()
        DatabaseManager.commit()

        self.user = User(
            platform_id=platform.id,
            platform_user_ident='U7200f33369e7e586c973c3a9df8feee4',
            last_seen=datetime.datetime.now()).add()

        DatabaseManager.commit()
Exemplo n.º 3
0
def parse_platform(platform_json, to_platform_id=None, source='platform_json'):
    """Parse Platform from platform definition.

    If *to_platform_id* is specified, update existing platform specified by
    *to_platform_id* instead of creating a new platform.

    If *to_platform_id* is a callable. The result of the call is used as the
    platform_id.
    """
    validate_platform_schema(platform_json)

    # Validate plaform-specific schema
    provider = get_messaging_provider(
        PlatformTypeEnum(platform_json['type_enum']))
    try:
        jsonschema.validate(platform_json['config'],
                            provider.get_config_schema())
    except jsonschema.exceptions.ValidationError:
        logger.error('Platform config validate failed for `%s\'!', source)
        raise

    if callable(to_platform_id):
        to_platform_id = to_platform_id(platform_json)

    bot_id = platform_json.get('bot_id')
    if bot_id:
        bot = Bot.get_by(id=bot_id,
                         account_id=platform_json['account_id'],
                         single=True)
        if not bot:
            raise RuntimeError('bot does not exist')

    if to_platform_id:
        # Update existing platform.
        logger.info(u'Updating existing Platform(id=%d, name=%s) from %s ...',
                    to_platform_id, platform_json['name'], source)
        platform = Platform.get_by(id=to_platform_id, single=True)
        platform.bot_id = bot_id
        platform.name = platform_json['name']
        platform.deployed = platform_json['deployed']
        platform.type_enum = platform_json['type_enum']
        platform.provider_ident = platform_json['provider_ident']
        platform.config = platform_json['config']
    else:
        # Create a new platform.
        logger.info(u'Creating new Platform(name=%s) from %s ...',
                    platform_json['name'], source)
        platform = Platform(**platform_json).add()

    DatabaseManager.flush()
    return platform
Exemplo n.º 4
0
    def test_schema_sanity(self):
        """Populate data into all tables and make sure there are no error."""
        DatabaseManager.reset()

        account = Account(name=u'Test Account',
                          email='*****@*****.**', passwd='test_hashed').add()

        bot = Bot(name=u'test', description=u'test', interaction_timeout=120,
                  session_timeout=86400).add()
        account.bots.append(bot)

        content = ContentModule(id='test', name='Content1', description='desc',
                                module_name='', ui_module_name='').add()
        parser = ParserModule(id='test', name='Parser1', description='desc',
                              module_name='passthrough', ui_module_name='',
                              variables={}).add()

        # Test for oauth schema
        oauth1 = OAuthInfo(provider=OAuthProviderEnum.Facebook,
                           provider_ident='mock-facebook-id').add()

        oauth2 = OAuthInfo(provider=OAuthProviderEnum.Github,
                           provider_ident='mock-github-id').add()

        account.oauth_infos.append(oauth1)
        account.oauth_infos.append(oauth2)
        DatabaseManager.commit()

        account_ = Account.get_by(id=account.id, single=True)
        self.assertNotEquals(account_, None)
        self.assertEquals(len(account_.oauth_infos), 2)

        oauth_ = OAuthInfo.get_by(provider_ident='mock-facebook-id',
                                  single=True)
        self.assertNotEquals(oauth_, None)
        self.assertNotEquals(oauth_.account, None)
        self.assertEquals(oauth_.account.id, account.id)

        # Test for bot
        account.bots.append(bot)

        DatabaseManager.commit()

        self.assertNotEquals(Account.get_by(id=account.id, single=True), None)
        self.assertNotEquals(Bot.get_by(id=bot.id, single=True), None)
        self.assertNotEquals(ContentModule.get_by(id=content.id, single=True),
                             None)
        self.assertNotEquals(ParserModule.get_by(id=parser.id, single=True),
                             None)

        # Check acccount_bot association table
        self.assertEquals(len(account.bots), 1)
        self.assertEquals(account.bots[0].id, bot.id)

        platform = Platform(name=u'Test platform',
                            bot_id=bot.id,
                            type_enum=PlatformTypeEnum.Facebook,
                            provider_ident='facebook_page_id',
                            config={}).add()
        DatabaseManager.commit()

        self.assertNotEquals(Platform.get_by(id=platform.id, single=True),
                             None)
        self.assertEquals(len(bot.platforms), 1)
        self.assertEquals(bot.platforms[0].id, platform.id)

        node1 = Node(stable_id='node1', name=u'1', bot_id=bot.id,
                     expect_input=True, content_module_id=content.id,
                     content_config={}, parser_module_id=parser.id,
                     parser_config={}).add()

        node2 = Node(stable_id='node2', name=u'2', bot_id=bot.id,
                     expect_input=True, content_module_id=content.id,
                     content_config={}, parser_module_id=parser.id,
                     parser_config={}).add()

        node3 = Node(stable_id='node3', name=u'3', bot_id=bot.id,
                     expect_input=True, content_module_id=content.id,
                     content_config={}, parser_module_id=parser.id,
                     parser_config={}).add()

        bot.orphan_nodes.append(node3)

        DatabaseManager.commit()

        self.assertNotEquals(Node.get_by(id=node1.id, single=True), None)
        self.assertNotEquals(Node.get_by(id=node2.id, single=True), None)
        self.assertNotEquals(Node.get_by(id=node3.id, single=True), None)

        # Test bot_node association table
        self.assertEquals(bot.orphan_nodes[0].id, node3.id)

        user = User(platform_id=platform.id,
                    platform_user_ident='',
                    last_seen=datetime.datetime.now()).add()
        DatabaseManager.commit()

        self.assertNotEquals(User.get_by(id=user.id, single=True), None)

        event = Event(bot_id=bot.id, user_id=user.id, event_name='event',
                      event_value={}).add()
        DatabaseManager.commit()

        self.assertNotEquals(Event.get_by(id=event.id, single=True), None)

        collected_datum = CollectedDatum(user_id=user.id,
                                         key='key', value={}).add()
        DatabaseManager.commit()

        self.assertNotEquals(CollectedDatum.get_by(id=collected_datum.id,
                                                   single=True), None)
        self.assertEquals(len(user.colleted_data), 1)
        self.assertEquals(user.colleted_data[0].id, collected_datum.id)

        conversation = Conversation(bot_id=bot.id, user_id=user.id,
                                    sender_enum=SenderEnum.Bot, msg={}).add()
        DatabaseManager.commit()
        self.assertNotEquals(Conversation.get_by(id=conversation.id,
                                                 single=True), None)

        # Broadcast
        bc = Broadcast(account_id=account.id, bot_id=bot.id,
                       name=u'New broadcast', messages=[],
                       scheduled_time=datetime.datetime.utcnow()).add()

        DatabaseManager.commit()
        self.assertNotEquals(Broadcast.get_by(id=bc.id, single=True), None)

        # PublicFeed, Feed
        account = Account(name=u'Test Account - 1',
                          email='*****@*****.**', passwd='test_hashed').add()
        feed1 = Feed(url='example.com/rss', type=FeedEnum.RSS,
                     title=u'foo.com', image_url='foo.com/logo').add()
        feed2 = Feed(url='example.com/rss', type=FeedEnum.RSS,
                     title=u'bar.com', image_url='bar.com/logo').add()
        feed3 = Feed(url='example.com/rss', type=FeedEnum.RSS,
                     title=u'baz.com', image_url='baz.com/logo').add()

        account.feeds.append(feed1)
        account.feeds.append(feed2)
        account.feeds.append(feed3)

        DatabaseManager.commit()
        self.assertNotEquals(Feed.get_by(id=feed1.id, single=True), None)

        feeds = Feed.search_title('ba')
        self.assertEquals(len(list(feeds)), 2)

        pfeed = PublicFeed(url='example.com/rss', type=FeedEnum.RSS,
                           title=u'example.com',
                           image_url='example.com/logo').add()

        DatabaseManager.commit()
        self.assertNotEquals(PublicFeed.get_by(id=pfeed.id, single=True), None)