def test_nearly_expired_token(self, now_func): n = datetime.datetime.now() now_func.side_effect = [n, n + datetime.timedelta(seconds=settings.TOKEN_VALIDITY_SECONDS - 60)] user = User.objects.create_user('testing', email='*****@*****.**') token = make_token(user, self.ip) self.assertEqual(check_token_get_user(token, self.ip), user)
def test_auth_echo(self): """ authenticated user connecting to anon socket """ self.assertEqual(User.objects.count(), 0) self.ws_close_properly = False test_case = self class WSClient(WebSocketClient): def on_open(self): self.write_message('hello') def on_message(self, data): # client is anon because an authenticated user connected to an anon handler test_case.delayed_assertions.extend([ (data, 'hello'), (len(all_clients.all_clients), 1), (len(all_clients.anon_clients), 1), (len(all_clients.auth_clients), 0), (str(all_clients), 'AllClients: 0 auth, 1 anon, 1 total') ]) test_case.ws_close_properly = True self.close() def on_close(self, code=None, reason=None): test_case.io_loop.add_callback(test_case.stop) user = User.objects.create_user('testing', email='*****@*****.**') token = make_token(user, '127.0.0.1') self.io_loop.add_callback(partial(WSClient, self.get_url('/ws/'), self.io_loop, token)) self.wait() self.assertTrue(self.ws_close_properly, 'websocket not closed properly') test_case.assertEqual(len(all_clients.all_clients), 0) test_case.assertEqual(len(all_clients.anon_clients), 0) test_case.assertEqual(len(all_clients.auth_clients), 0)
def test_invalid_time_stamp_token(self): user = User.objects.create_user('testing', email='*****@*****.**') token = make_token(user, self.ip) secs, uid, hash = token.split('-') new_secs = base36_to_int(secs) - 10 new_secs = int_to_base36(new_secs) wrong_token = '%s-%s-%s' % (new_secs, uid, hash) self.assertFalse(check_token_get_user(wrong_token, self.ip))
def test_ip_change_token(self): user = User.objects.create_user('testing', email='*****@*****.**') token = make_token(user, self.ip) self.assertFalse(check_token_get_user(token, '127.0.0.2'))
def test_invalid_base64_token(self): user = User.objects.create_user('testing', email='*****@*****.**') token = make_token(user, self.ip) secs, uid, hash = token.split('-') wrong_token = '%s-%s-%s' % (secs, '@;[]_+.', hash) self.assertFalse(check_token_get_user(wrong_token, self.ip))
def test_valid_token(self): user = User.objects.create_user('testing', email='*****@*****.**') token = make_token(user, self.ip) self.assertEqual(check_token_get_user(token, self.ip), user)