Exemplo n.º 1
0
def test_can_use_existing():
    t = MockTime()
    a = AuthService(time=t)
    token = a.create_token('xyz', 'data', lifetime=10)
    t.tick(5)
    assert a.pop_token('xyz', token) == 'data'
    assert a.pop_token('xyz', token) is None
Exemplo n.º 2
0
def test_msnp_commands():
    user_service = UserService()
    auth_service = AuthService()

    nb = NB(user_service, auth_service, [Service('0.0.0.0', 0)])
    sb = SB(user_service, auth_service)

    # User 1 login
    nc1 = NBConn(nb, MSNPWriter())
    user1 = _login_msnp(nc1, '*****@*****.**')
    nc1._l_adc(1, 'FL', '[email protected]', 'F=Test2')
    nc1.writer.pop_message('ADC', 1, 'FL', '[email protected]', ANY)

    # User 2 login
    nc2 = NBConn(nb, MSNPWriter())
    user2 = _login_msnp(nc2, '*****@*****.**')
    nc1.writer.pop_message('NLN', 'NLN', '*****@*****.**', ANY, ANY, ANY)
    nc1.writer.pop_message('UBX', '*****@*****.**', ANY)
    nc2.writer.pop_message('ILN', 5, 'NLN', '*****@*****.**', ANY, ANY, ANY)
    nc2.writer.pop_message('UBX', '*****@*****.**', ANY)

    # User 1 starts convo
    nc1._l_xfr(1, 'SB')
    msg = nc1.writer.pop_message('XFR', 1, 'SB', ANY, 'CKI', ANY)
    token = msg[-1]

    sc1 = SBConn(sb, nb, MSNPWriter())
    sc1._a_usr(0, '*****@*****.**', token)
    sc1.writer.pop_message('USR', 0, 'OK', '*****@*****.**', ANY)
    assert sc1.user.uuid == user1.uuid

    sc1._l_cal(1, 'doesnotexist')
    sc1.writer.pop_message(Err.InternalServerError, 1)

    sc1._l_cal(2, '*****@*****.**')
    sc1.writer.pop_message('CAL', 2, 'RINGING', ANY)

    msg = nc2.writer.pop_message('RNG', ANY, ANY, 'CKI', ANY, user1.email, ANY)
    sbsess_id = msg[1]
    token = msg[4]

    # User 2 joins convo
    sc2 = SBConn(sb, nb, MSNPWriter())
    sc2._a_ans(0, '*****@*****.**', token, sbsess_id)
    sc2.writer.pop_message('IRO', 0, 1, 1, '*****@*****.**', ANY)
    sc2.writer.pop_message('ANS', 0, 'OK')
    sc1.writer.pop_message('JOI', '*****@*****.**', ANY)

    # User 1 sends message
    sc1._l_msg(3, 'A', b"my message")
    sc1.writer.pop_message('ACK', 3)
    sc2.writer.pop_message('MSG', '*****@*****.**', ANY, b"my message")
Exemplo n.º 3
0
def test_multiple_out_of_order():
    t = MockTime()
    a = AuthService(time=t)
    token1 = a.create_token('xyz', 'data1', lifetime=10)
    t.tick(5)
    token2 = a.create_token('abc', 'data2', lifetime=15)
    t.tick(3)
    assert a.pop_token('abc', token2) == 'data2'
    t.tick(1)
    assert a.pop_token('xyz', token1) == 'data1'
Exemplo n.º 4
0
def test_cant_use_wrong_purpose():
    t = MockTime()
    a = AuthService(time=t)
    token = a.create_token('xyz', 'data', lifetime=10)
    assert a.pop_token('zyx', token) is None
    assert a.pop_token('xyz', token) is None
Exemplo n.º 5
0
def test_cant_use_expired():
    t = MockTime()
    a = AuthService(time=t)
    token = a.create_token('xyz', 'data', lifetime=10)
    t.tick(11)
    assert a.pop_token('xyz', token) is None
Exemplo n.º 6
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     self.stats = None
     self.server_status = None
     self.auth_service = AuthService()
Exemplo n.º 7
0
def test_msnp_commands():
    user_service = UserService()
    auth_service = AuthService()
    nb = NB(user_service, auth_service, [Service('0.0.0.0', 0)])

    email = '*****@*****.**'
    uuid = user_service.get_uuid(email)

    w1 = MSNPWriter()
    nc1 = NBConn(nb, w1)

    # Login

    nc1._a_ver(0, 'MSNP12')
    w1.pop_message('VER', 0, 'MSNP12')
    nc1._a_cvr(1, 'a0', 'a1', 'a2', 'a3', 'a4', 'a5')
    w1.pop_message('CVR', 1, 'a5', 'a5', 'a5', ANY, ANY)
    nc1._a_usr(3, 'TWN', 'I', email)
    w1.pop_message('USR', 3, 'TWN', 'S', ANY)

    token = auth_service.create_token('nb/login', uuid)
    nc1._a_usr(4, 'TWN', 'S', token)
    w1.pop_message('USR', 4, 'OK', email, '1', '0')

    user = nc1.user

    nc1._l_syn(5)
    w1.pop_message('SYN', 5, ANY, ANY, ANY, ANY)
    w1.pop_message('GTC', 'A')
    w1.pop_message('BLP', 'AL')
    w1.pop_message('PRP', 'MFN', ANY)
    w1.assert_empty()

    # Group/Contact Management

    nc1._l_adg(8, 'x' * 100, 'ignored')
    w1.pop_message(Err.GroupNameTooLong, 8)
    nc1._l_adg(9, "New Group")
    msg = w1.pop_message('ADG', 9, "New Group", ANY, ANY)
    assert user.detail.groups[msg[3]].name == msg[2]
    nc1._l_rmg(10, "New%20Group")
    w1.pop_message('RMG', 10, 1, msg[3])
    assert msg[3] not in user.detail.groups

    nc1._l_rmg(11, '0')
    w1.pop_message(Err.GroupZeroUnremovable, 11)
    nc1._l_rmg(12, 'blahblahblah')
    w1.pop_message(Err.GroupInvalid, 12)

    nc1._l_adg(13, "Group Name")
    msg = w1.pop_message('ADG', 13, "Group Name", ANY, ANY)
    group_uuid = msg[3]
    assert user.detail.groups[group_uuid].name == msg[2]
    nc1._l_reg(14, group_uuid, "New Group Name", 'ignored')
    w1.pop_message('REG', 14, 1, "New Group Name", group_uuid, ANY)
    assert user.detail.groups[group_uuid].name == "New Group Name"

    nc1._l_adc(15, 'FL', 'N=doesnotexist')
    w1.pop_message(Err.InvalidPrincipal, 15)
    nc1._l_adc(16, 'FL', '[email protected]', 'F=Test2')
    msg = w1.pop_message('ADC', 16, 'FL', '[email protected]', ANY)
    uuid = msg[-1][2:]
    assert user.detail.contacts[uuid].head.email == '*****@*****.**'
    assert user.detail.contacts[uuid].lists == Lst.FL
    assert not user.detail.contacts[uuid].groups
    nc1._l_adc(17, 'FL', 'C={}'.format(uuid), group_uuid)
    w1.pop_message('ADC', 17, 'FL', 'C={}'.format(uuid), group_uuid)
    assert user.detail.contacts[uuid].groups == {group_uuid}

    assert nb._unsynced_db, "has unsynced data"
    nb._sync_db_impl()
    assert not nb._unsynced_db, "db was synced"

    nc1._l_adc(18, 'BL', '[email protected]')
    w1.pop_message('ADC', 18, 'BL', '[email protected]')
    assert user.detail.contacts[uuid].lists == Lst.FL | Lst.BL
    nc1._l_rem(19, 'BL', '*****@*****.**')
    w1.pop_message('REM', 19, 'BL', '*****@*****.**')
    assert user.detail.contacts[uuid].lists == Lst.FL
    nc1._l_rem(20, 'FL', uuid, 'notvalidgroupid')
    w1.pop_message(Err.GroupInvalid, 20)
    nc1._l_rem(21, 'FL', uuid, group_uuid)
    w1.pop_message('REM', 21, 'FL', uuid, group_uuid)
    assert not user.detail.contacts[uuid].groups
    nc1._l_rem(22, 'FL', 'notvaliduserid')
    w1.pop_message(Err.PrincipalNotOnList, 22)
    nc1._l_rem(23, 'FL', uuid)
    w1.pop_message('REM', 23, 'FL', uuid)
    assert uuid not in user.detail.contacts

    # Misc

    nc1._l_png()
    w1.pop_message('QNG', ANY)

    nc1._l_url(6, 'blah')
    w1.pop_message('URL', 6, ANY, ANY, ANY)

    nc1._l_uux(7, {
        'PSM': "my message",
        'CurrentMedia': "song name",
    })
    w1.pop_message('UUX', 7, ANY)
    assert user.status.message == "my message"
    assert user.status.media == "song name"

    nc1._l_gtc(24, 'Y')
    w1.pop_message('GTC', 24, 'Y')

    nc1._l_blp(25, 'AL')
    w1.pop_message('BLP', 25, 'AL')

    nc1._l_chg(26, 'NLN', 0)
    w1.pop_message('CHG', 26, 'NLN', 0)
    assert user.status.substatus == Substatus.NLN

    nc1._l_rea(27, '*****@*****.**', "My Name")
    w1.pop_message(Err.CommandDisabled, 27)

    nc1._l_snd(28, '*****@*****.**')
    w1.pop_message('SND', 28, '*****@*****.**')

    nc1._l_prp(29, 'MFN', "My Name")
    w1.pop_message('PRP', 29, 'MFN', "My Name")
    assert user.status.name == "My Name"

    nc1._l_sbp(30, uuid, 'MFN', "Buddy Name")
    w1.pop_message('SBP', 30, uuid, 'MFN', "Buddy Name")

    nc1._l_xfr(31, 'SB')
    w1.pop_message('XFR', 31, 'SB', ANY, 'CKI', ANY)