def test_auth(self): # Find the foo service as user test1 foo_service_1 = SERVICE_MANAGER.find_by_id( self.foo_host, self.foo_id, auth=FooBarAuthSameTest.Auth("test1", "password1") ) foos = foo_service_1.bind("foos") foos.create({"bar_id": 1, "bar_server": "http://crossref-bar-server", "bar_instance": ""}) foos.pull() connmgr_conns = CONNECTION_MANAGER.conns # One connection to the foo server self.assertEqual(len(connmgr_conns), 1) # Follow a link to the bar service bar = foos[0].follow("bar") bar_service_1 = bar.service self.assertEqual(bar.data, "Bar-1") self.assertEqual(bar_service_1.auth.username, "test1") # Now a second connection -- to the bar server as test1 self.assertEqual(len(connmgr_conns), 2) # Follow it again -- we should end up with a *different* service # object, but the same number of connections and same username bar = foos[0].follow("bar") bar_service_2 = bar.service self.assertEqual(bar.data, "Bar-1") self.assertEqual(bar_service_2.auth.username, "test1") self.assertEqual(len(connmgr_conns), 2) self.assertNotEqual(bar_service_1, bar_service_2) # Now establish switch to user 'test2' foo_service_1 = SERVICE_MANAGER.find_by_id( self.foo_host, self.foo_id, auth=FooBarAuthSameTest.Auth("test2", "password2") ) foos = foo_service_1.bind("foos") foos.create({"bar_id": 1, "bar_server": "http://crossref-bar-server", "bar_instance": ""}) foos.pull() connmgr_conns = CONNECTION_MANAGER.conns # A third connection is established self.assertEqual(len(connmgr_conns), 3) # Try to follow a link to the bar service, this should fail # as the username 'test2' is not allowed on the bar server with self.assertRaises(UnknownUsername): bar = foos[0].follow("bar") # Note that simply following the link does not actually create # a connection, have to access data... bar.data # Will have a 4th connection, even though our auth is bad self.assertEqual(len(connmgr_conns), 4)
def test_bad_password(self): service = SERVICE_MANAGER.find_by_id( self.host, self.id, auth=BasicAuthTest.Auth('test', 'badpassword')) x = service.bind('x') x.data = 3 with self.assertRaises(BadPassword): x.push()
def setUp(self): self.id = 'http://support.riverbed.com/apis/basic/1.0' self.host = 'http://basic-server:80' TEST_SERVER_MANAGER.reset() self.server = TEST_SERVER_MANAGER.register_server( self.host, self.id, None, BasicServer, self) self.service = SERVICE_MANAGER.find_by_id(self.host, self.id)
def test_missing_auth(self): auth = FooBarAuthDiffTest.Auth() auth.add_auth("crossref-foo-server", "test1", "password1") # Find the foo service as user test1 foo_service_1 = SERVICE_MANAGER.find_by_id(self.foo_host, self.foo_id, auth=auth) foos = foo_service_1.bind("foos") foos.create({"bar_id": 1, "bar_server": "http://crossref-bar-server", "bar_instance": ""}) # Try to follow a link to the bar service with self.assertRaises(MissingAuthHeader): bar = foos[0].follow("bar") bar.data
def test_foo(self): id = "http://support.riverbed.com/apis/crossref.foo/1.0" self.foo_service = SERVICE_MANAGER.find_by_id("http://crossref-foo-server", id) foos = self.foo_service.bind("foos") for i in range(2): foos.create({"bar_id": i + 1, "bar_server": "http://crossref-bar-server-%d" % (i + 1), "bar_instance": ""}) for j in range(2): foos.create( { "bar_id": i + 1, "bar_server": "http://crossref-bar-server-%d" % (i + 1), "bar_instance": "instance-%d" % (j + 1), } ) foos.pull() self.assertEqual(type(foos), ListDataRep) self.assertEqual(foos[0]["id"].data, 1) # There should be one and only one connection to the foo-server self.assertEqual(len(CONNECTION_MANAGER.conns), 1) self.assertTrue(("http://crossref-foo-server", None) in CONNECTION_MANAGER.conns) bar = foos[0].follow("bar") self.assertEqual(bar.data, "Bar-1") for foo in foos: bar_id = foo.data["bar_id"] bar_instance = foo.data["bar_instance"] bar = foo.follow("bar") self.assertEqual(bar.service.host, "http://crossref-bar-server-%s" % bar_id) if bar_instance: self.assertEqual(bar.data, "Bar-%s-%s" % (bar_instance, bar_id)) else: self.assertEqual(bar.data, "Bar-%s" % bar_id) # After following bar, each one should go do a new server, conns = CONNECTION_MANAGER.conns self.assertEqual(len(conns), 3) self.assertTrue(("http://crossref-foo-server", None) in conns) self.assertTrue(("http://crossref-bar-server-1", None) in conns) self.assertTrue(("http://crossref-bar-server-2", None) in conns) self.assertFalse(("http://crossref-bar-server-3", None) in conns)
def test_auth(self): auth = FooBarAuthDiffTest.Auth() auth.add_auth("crossref-foo-server", "test1", "password1") auth.add_auth("crossref-bar-server", "test1", "password2") # Find the foo service as user test1 foo_service_1 = SERVICE_MANAGER.find_by_id(self.foo_host, self.foo_id, auth=auth) foos = foo_service_1.bind("foos") foos.create({"bar_id": 1, "bar_server": "http://crossref-bar-server", "bar_instance": ""}) foos.pull() connmgr_conns = CONNECTION_MANAGER.conns # One connection to the foo server self.assertEqual(len(connmgr_conns), 1) # Follow a link to the bar service bar = foos[0].follow("bar") bar_service_1 = bar.service self.assertEqual(bar.data, "Bar-1") self.assertEqual(bar_service_1.auth.auth["crossref-bar-server"][0], "test1") # Now a second connection -- to the bar server as test1 self.assertEqual(len(connmgr_conns), 2)
def test_embed_bar(self): id = "http://support.riverbed.com/apis/crossref.foo/1.0" self.foo_service = SERVICE_MANAGER.find_by_id("http://crossref-foo-server", id) embed_bar = self.foo_service.bind("embed_bar") print embed_bar
def test_auth(self): service = SERVICE_MANAGER.find_by_id( self.host, self.id, auth=BasicAuthTest.Auth('test', 'password')) x = service.bind('x') x.data = 3 x.push()