'A comprehensive security test for the access_stream_by_* API functions.'

def test_access_stream(self):

    hamlet = self.example_user('hamlet')
    hamlet_email = hamlet.email
    stream_name = 'new_private_stream'
    self.login(hamlet_email)
    self.common_subscribe_to_streams(hamlet_email, [stream_name], invite_only=True)
    stream = get_stream(stream_name, hamlet.realm)
    othello = self.example_user('othello')
    with self.assertRaisesRegex(JsonableError, 'Invalid    stream    id'):
        access_stream_by_id(hamlet, 501232)
    with self.assertRaisesRegex(JsonableError, "Invalid    stream    name    'invalid    stream'"):
        access_stream_by_name(hamlet, 'invalid    stream')
    (stream_ret, rec_ret, sub_ret) = access_stream_by_id(hamlet, stream.id)
    self.assertEqual(stream, stream_ret)
    self.assertEqual(sub_ret.recipient, rec_ret)
    self.assertEqual(sub_ret.recipient.type_id, stream.id)
    (stream_ret2, rec_ret2, sub_ret2) = access_stream_by_name(hamlet, stream.name)
    self.assertEqual(stream_ret, stream_ret2)
    self.assertEqual(sub_ret, sub_ret2)
    self.assertEqual(rec_ret, rec_ret2)
    with self.assertRaisesRegex(JsonableError, 'Invalid    stream    id'):
        access_stream_by_id(othello, stream.id)
    with self.assertRaisesRegex(JsonableError, "Invalid    stream    name    'new_private_stream'"):
        access_stream_by_name(othello, stream.name)
    public_stream_name = 'public_stream'
    self.common_subscribe_to_streams(hamlet_email, [public_stream_name], invite_only=False)
    public_stream = get_stream(public_stream_name, hamlet.realm)
    access_stream_by_id(othello, public_stream.id)
    access_stream_by_name(othello, public_stream.name)
    access_stream_by_id(hamlet, public_stream.id)
    access_stream_by_name(hamlet, public_stream.name)
    mit_realm = get_realm('zephyr')
    (mit_stream, _) = create_stream_if_needed(mit_realm, 'mit_stream', invite_only=False)
    sipbtest = self.mit_user('sipbtest')
    with self.assertRaisesRegex(JsonableError, 'Invalid    stream    id'):
        access_stream_by_id(hamlet, mit_stream.id)
    with self.assertRaisesRegex(JsonableError, "Invalid    stream    name    'mit_stream'"):
        access_stream_by_name(hamlet, mit_stream.name)
    with self.assertRaisesRegex(JsonableError, 'Invalid    stream    id'):
        access_stream_by_id(sipbtest, stream.id)
    with self.assertRaisesRegex(JsonableError, "Invalid    stream    name    'new_private_stream'"):
        access_stream_by_name(sipbtest, stream.name)
    with self.assertRaisesRegex(JsonableError, 'Invalid    stream    id'):
        access_stream_by_id(sipbtest, mit_stream.id)
    with self.assertRaisesRegex(JsonableError, "Invalid    stream    name    'mit_stream'"):
        access_stream_by_name(sipbtest, mit_stream.name)
    self.common_subscribe_to_streams(sipbtest.email, [mit_stream.name])
    access_stream_by_id(sipbtest, mit_stream.id)
    access_stream_by_name(sipbtest, mit_stream.name)
