def setUp(self): self.user1 = User.objects.create(username='******', is_staff=True, is_superuser=True) self.user1.set_password('1234') self.user1.save() self.user2 = User.objects.create(username='******') self.user2.set_password('1234') self.user2.save() self.user3 = User.objects.create(username='******') self.user3.set_password('1234') self.user3.save() self.client1 = Client() self.client2 = Client() self.client3 = Client() self.server = Server() self.server.start() self.assertTrue(TestRegistrationEvent in EVENTS)
class ServerTest(TransactionTestCase): """This test must be a TransactionalTestCase because we're accessing the db in multiple threads.""" def setUp(self): self.user1 = User.objects.create(username='******', is_staff=True, is_superuser=True) self.user1.set_password('1234') self.user1.save() self.user2 = User.objects.create(username='******') self.user2.set_password('1234') self.user2.save() self.user3 = User.objects.create(username='******') self.user3.set_password('1234') self.user3.save() self.client1 = Client() self.client2 = Client() self.client3 = Client() self.server = Server() self.server.start() self.assertTrue(TestRegistrationEvent in EVENTS) def tearDown(self): self.server.stop() def test_server_setup(self): self.client1.login(username='******', password='******') self.client2.login(username='******', password='******') self.client3.login(username='******', password='******') self.failUnlessEqual(ServerRegistration.objects.all().count(), 1) self.failUnlessEqual(ServerRegistration.objects.all()[0], self.server.registration) event_handler = EventHandler() client = WSClient(self.user1.session_key, '127.0.0.1', self.server.ws_server.port, '127.0.0.1:8000', event_handler=event_handler.handle_event) client.authenticate() event = event_handler.events.get(True, 3) self.failUnless(event.authenticated) self.assertEqual('trevor', event.username) self.assertEqual('trevor', client.username) event_handler2 = EventHandler() client2 = WSClient(self.user2.session_key, '127.0.0.1', self.server.ws_server.port, '127.0.0.1:8000', event_handler=event_handler2.handle_event) client2.authenticate() event = event_handler2.events.get(True, 3) self.failUnless(event.authenticated) self.assertEqual('alice', event.username) self.assertEqual('alice', client2.username) event_handler3 = EventHandler() client3 = WSClient(self.user3.session_key, '127.0.0.1', self.server.ws_server.port, '127.0.0.1:8000', event_handler=event_handler3.handle_event) client3.authenticate() event = event_handler3.events.get(True, 3) self.failUnless(event.authenticated) client.request_server_info() event = event_handler.events.get(True, 3) self.failUnless(event.infos) self.failUnless(event.infos['status']) self.assertEqual(event.infos['status'], 'ok') client.list_channels() event = event_handler.events.get(True, 3) self.failUnless(isinstance(event, ChannelList)) self.assertEqual(len(event.channels), 1) client.create_channel('dart.channel') event = event_handler.events.get(True, 3) self.failUnless(isinstance(event, ChannelCreated)) self.assertEqual(event.channel_id, 'dart.channel') client.list_channels() event = event_handler.events.get(True, 3) self.assertEqual(len(event.channels), 2) self.failUnless('dart.channel' in event.channels) client.create_channel('kite.channel', class_name='wind.tests.server_tests.TestChannel') event = event_handler.events.get(True, 3) self.failUnless(isinstance(event, ChannelCreated)) self.assertEqual(event.channel_id, 'kite.channel') client.list_channels() event = event_handler.events.get(True, 3) self.assertEqual(len(event.channels), 3) self.failUnless('dart.channel' in event.channels) self.failUnless('kite.channel' in event.channels) client.send_event(TestBroadcastEvent('I love traffic lights.')) self.assertRaises(Queue.Empty, event_handler.events.get, True, 3) self.assertFalse(client.request_channel_info()) client.subscribe('kite.channel') event = event_handler.events.get(True, 3) self.failUnless(isinstance(event, SubscribeResponse)) self.assertEqual('kite.channel', event.channel_id) self.assertTrue(event.joined) client3.subscribe('kite.channel') event = event_handler3.events.get(True, 3) self.failUnless(isinstance(event, SubscribeResponse)) self.assertEqual('kite.channel', event.channel_id) self.assertFalse(event.joined) # This is false because the TestChannel rejects bob self.assertTrue(client.request_channel_info()) event = event_handler.events.get(True, 3) self.failUnless(isinstance(event, ChannelInfo)) self.assertEqual(len(event.usernames), 1) self.assertEqual(event.usernames[0], 'trevor', 'Unexpected usernames: %s' % event.usernames) client.send_event(TestBroadcastEvent('I love traffic lights.')) event = event_handler.events.get(True, 3) self.assertEqual(event.message, 'I love traffic lights.') self.assertEqual(event.username, 'trevor') self.assertRaises(Queue.Empty, event_handler2.events.get, True, 3) client.delete_channel('dart.channel') event = event_handler.events.get(True, 3) self.failUnless(isinstance(event, ChannelDeleted)) self.assertEqual(event.channel_id, 'dart.channel') client.list_channels() event = event_handler.events.get(True, 3) self.assertEqual(len(event.channels), 2) self.failIf('dart.channel' in event.channels) self.failUnless('kite.channel' in event.channels) self.assertEqual('kite.channel', client.channel_id) ServerRegistration.objects.broadcast_event(self.user1.session_key, TestBroadcastEvent("Hey, y'all!"), 'kite.channel') event = event_handler.events.get(True, 3) self.failUnlessEqual(event.message, "Hey, y'all!") self.assertRaises(Queue.Empty, event_handler2.events.get, True, 3) client2.subscribe('kite.channel') event = event_handler2.events.get(True, 3) self.failUnless(isinstance(event, SubscribeResponse)) self.assertEqual('kite.channel', event.channel_id) self.assertTrue(event.joined) self.assertTrue(client.request_channel_info()) event = event_handler.events.get(True, 3) self.failUnless(isinstance(event, ChannelInfo)) self.assertEqual(len(event.usernames), 2, event.usernames) self.assertTrue('trevor' in event.usernames) self.assertTrue('alice' in event.usernames) ServerRegistration.objects.broadcast_event(self.user1.session_key, TestBroadcastEvent("Hey, y'all!"), 'kite.channel') event = event_handler.events.get(True, 3) self.failUnlessEqual(event.message, "Hey, y'all!") event = event_handler2.events.get(True, 3) self.failUnlessEqual(event.message, "Hey, y'all!") client.close() client2.close()