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()
def setUp(self): DatabaseManager.connect() DatabaseManager.reset() app.test_client_class = BearerAuthTestClient self.app = app.test_client() self.setup_prerequisite()
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()
def setUp(self): app.config['TESTING'] = True DatabaseManager.connect() DatabaseManager.reset() app.test_client_class = BearerAuthTestClient self.app = app.test_client() self.bot_ids = [] self.broadcast_ids = [] self.setup_prerequisite()
def test_json_serializer(self): DatabaseManager.reset() account = Account(name=u'tester', email='*****@*****.**') dt = datetime.datetime(2010, 1, 1, 0, 0, tzinfo=pytz.utc) account.created_at = dt account.__json_public__.append('created_at') d = account.to_json() self.assertEquals(d['created_at'], 1262304000) self.assertEquals(d['name'], 'tester') self.assertEquals(d['email'], '*****@*****.**')
def reset_and_setup_bots(bot_names): """Reset database and setup bots. Args: bot_names: a list of bot names to setup """ bots = [] DatabaseManager.reset() register_all_modules() for platform_name in ['dev/bb8.test.platform', 'dev/bb8.test2.platform']: parse_platform_from_file(get_platform_filename(platform_name)) for bot_name in bot_names: bots.append(parse_bot_from_file(get_bot_filename(bot_name))) return bots
def test_auth(self): DatabaseManager.reset() account = Account(name=u'Test Account 3', email='*****@*****.**').add() some_passwd = 'abcdefg' account.set_passwd(some_passwd) DatabaseManager.commit() account_ = Account.get_by(id=account.id, single=True) self.assertNotEquals(account_.passwd, some_passwd) self.assertEquals(account_.verify_passwd(some_passwd), True) self.assertEquals(account_.verify_passwd('should be wrong'), False) token = account_.auth_token account_t = Account.from_auth_token(token) self.assertEquals(account_.id, account_t.id) fake_token = jwt.encode({ 'iss': 'compose.ai', 'sub': account_.id, 'jti': str(uuid.uuid4()), 'iat': datetime.datetime.utcnow(), 'exp': datetime.datetime.utcnow() + datetime.timedelta(days=14) }, 'im fake secret') with self.assertRaises(RuntimeError): Account.from_auth_token(fake_token) outdated_token = jwt.encode({ 'iss': 'compose.ai', 'sub': account_.id, 'jti': str(uuid.uuid4()), 'iat': datetime.datetime.utcnow() - datetime.timedelta(days=30), 'exp': datetime.datetime.utcnow() - datetime.timedelta(days=15) }, config.JWT_SECRET) with self.assertRaises(RuntimeError): Account.from_auth_token(outdated_token)
def test_Bot_API(self): """Test Bot model APIs.""" DatabaseManager.reset() bots = reset_and_setup_bots(['test/simple.bot', 'test/postback.bot']) bot1 = bots[0] bot2 = bots[1] bot2_node_len = len(bot2.nodes) bot1.delete_all_nodes() DatabaseManager.commit() # All nodes and links related to this bot should be gone. self.assertEquals(bot1.nodes, []) # Make sure delete_all_nodes does not accidentally delete node # of other bot self.assertEquals(len(bot2.nodes), bot2_node_len) # Test bot reconstruction parse_bot_from_file(get_bot_filename('test/simple.bot'), bot1.id) DatabaseManager.commit() self.assertNotEquals(bot1.nodes, []) self.assertEquals(bot1.users, []) User(platform_id=bot1.platforms[0].id, platform_user_ident='blablabla', last_seen=datetime.datetime.now()).add() User(platform_id=bot1.platforms[1].id, platform_user_ident='blablabla2', last_seen=datetime.datetime.now()).add() DatabaseManager.commit() self.assertEquals(len(bot1.users), 2) bot1_id = bot1.id bot1.delete() DatabaseManager.commit() self.assertEquals(Bot.get_by(id=bot1_id, single=True), None)
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)
def test_schema(self): """Test database schema and make sure all the tables can be created without problems.""" DatabaseManager.reset() DatabaseManager.commit()
def setUp(self): DatabaseManager.connect() DatabaseManager.reset()
def run(self): # pylint: disable=E0202 with DatabaseSession(): DatabaseManager.reset() modules.register_all_modules() platform_parser.build_all_platforms(include_dev=True) bot_parser.build_all_bots()
def setUp(self): DatabaseManager.connect() DatabaseManager.reset() self.setup_prerequisite()