def test_multilogin(monkeypatch): # TODO: Test switch_to acct1, acct2 = Record.new(), Record.new() ids = {acct1.id, acct2.id} acid, token1, token2 = (utils.unique_hash() for _ in range(3)) # Save the first token auth.cookies.save(acct=acct1, token=token1, acid=acid) # Save the second token (with different lifetime) auth.cookies.save(acct=acct2, token=token2, acid=acid, session=True) # Both tokens must work correctly current = auth.Current(token=token1, acid=acid) assert current.loggedin assert current.record == acct1 assert current.multi assert {x.id for x in current.records} == ids current = auth.Current(token=token2, acid=acid) assert current.loggedin assert current.record == acct2 assert current.multi assert {x.id for x in current.records} == ids # Test tokens' lifetime monkeypatch.setattr("time.time", session_time) current = auth.Current(token=token2, acid=acid) assert not current.loggedin assert not current.multi monkeypatch.setattr("time.time", cookie_time) current = auth.Current(token=token1, acid=acid) assert not current.loggedin assert not current.multi
def test_notifications(nt): this, other = Record.new(), Record.new() klass = getattr(no, nt) o = klass.new(this, other=other) assert o.good() assert o.id assert list(no.load(this)) no.Notification.delete(this, [o.id]) assert not list(no.load(this))
def test_g(self, by): other, this = Record.new(), Record.new() req = application.get('/api/02/records?{}={}'.format(by, getattr(other, by))) assert req.status_int == 200 assert req.json['success'], req.json assert req.json['record'].keys() <= other.as_public_dict().keys() for name, value in this.cookies.items(): application.set_cookie(name, value) req = application.get('/api/02/records') assert req.status_int == 200 assert req.json['success'], req.json assert req.json['record'].keys() <= this.as_public_dict().keys()
def test_put(self, api_authed, act, by): acct = Record.new() req = application.put_json( '/api/02/relations/{}?{}={}'.format(act, by, getattr(acct, by)) ) assert req.status_int == 200 assert req.json['success'], req.json
def test_feedgetters(): this, *other = (Record.new() for _ in range(3)) for x in other: relations.follow(x, this) it = relations.feedgetters(this) assert sum(1 for _ in it) == len(other)
def test_current(monkeypatch): account = Record.new() acid, *tokens = (utils.unique_hash() for _ in range(3)) # Split the list (aproximatly) in half and save the tokens cookies, sessions = [tokens[0]], [tokens[-1]] for x in cookies: auth.cookies.save(acct=account, acid=acid, token=x) for x in sessions: auth.cookies.save(acct=account, acid=acid, token=x, session=True) # Test the attributes and the return value for token in tokens: current = auth.Current(token=token, acid=acid) assert current.loggedin assert not current.multi assert current.record == account # TODO: Be consistent and use `patch` from `unittest.mock`? # Test session lifetime monkeypatch.setattr("time.time", session_time) for token in sessions: current = auth.Current(token=token, acid=acid) assert not current.loggedin assert not current.multi # Test cookie lifetime monkeypatch.setattr("time.time", cookie_time) for token in cookies: current = auth.Current(token=token, acid=acid) assert not current.loggedin assert not current.multi
def test_blocked(): this, other = (Record.new() for _ in range(2)) assert not relations.is_blocked(this, other) relations.block(this, other) assert relations.is_blocked(this, other) relations.unblock(this, other) assert not relations.is_blocked(this, other)
def wrapper(*a, **ka): args = signature(func).bind_partial(*a, **ka).arguments if 'acct' not in args: args['acct'] = Record.new() if 'images' not in args: args['images'] = [next(opened)] return func(**args)
def wrapper(*a, **ka): args = signature(func).bind_partial(*a, **ka).arguments if 'poster' not in args: args['poster'] = Record.new() if 'content' not in args: args['content'] = '<string>' return func(**args)
def test_nonlogged(self): acct = Record.new() prof = application.get('/' + acct.name) assert prof.status_int == 200 prof.mustcontain(acct.name) unk = application.get('/' + uuid.uuid4().hex, expect_errors=True) assert unk.status_int == 404
def test_downloading(url): acct = Record.new() # Working links downloaded = list(Image.new(acct, [url], allow_gif=True)) assert len(downloaded) assert len(downloaded) == 1 assert all(isinstance(x, Image) for x in downloaded) assert all(x.owner == acct for x in downloaded) assert all(x.good() for x in downloaded)
def test_profile(): acct = Record.new() res = profile.profile(acct, minimal=False) assert 'avatar' in res assert 'website_link' in res assert 'country_name' in res res = profile.profile(acct) assert 'avatar' in res assert 'website_link' not in res assert 'country_name' not in res
def test_cookies(): account = Record.new() # No distinction between real cookies and session acid, *tokens = (utils.unique_hash() for _ in range(3)) for token in tokens: auth.cookies.save(acct=account, acid=acid, token=token) it = auth.cookies.get_by_acid(acid) assert sum(1 for _ in it) == len(tokens) auth.cookies.delete_tokens(tokens) assert next(auth.cookies.get_by_acid(acid), None) is None
def test_get(self, api_authed): other = Record.new() # These types do no require anything other than `other` types = (notifications.FollowerNotification, notifications.FriendNotification) ns = [t.new(api_authed, other=other) for t in types] req = application.get('/api/02/notifications') assert req.status_int == 200 assert req.json['success'], req.json assert {str(x.id) for x in ns} == {x['id'] for x in req.json['notifications']} # TODO: Make `text` not empty in tests assert {x.get_text() for x in ns} == {x['text'] for x in req.json['notifications']}
def test_post(self): acct = Record.new() params = {'url': '/auth/sign-in', 'params': {'name': acct.name, 'pwd': '<string>'}} # Test wrong name, pwd combination resp = application.post(**params) assert resp.status_int == 200 # Succesful sign up params['params']['pwd'] = acct.plaintext_pwd resp = application.post(**params) assert resp.status_int == 302
def test_followers_and_friends(): this, other = (Record.new() for _ in range(2)) relations.follow(other, this) assert relations.is_follower(other, this) assert not relations.are_friends(other, this) relations.follow(this, other) assert relations.is_follower(this, other) assert relations.are_friends(other, this) relations.unfollow(this, other) assert not relations.is_follower(this, other) assert not relations.are_friends(other, this)
def test_delete(self, api_authed): other = Record.new() # These types do no require anything other than `other` types = (notifications.FollowerNotification, notifications.FriendNotification) ns = [t.new(api_authed, other=other) for t in types] req = application.delete_json('/api/02/notifications', params={'ids': [str(ns[0].id)]}) assert req.status_int == 200 assert req.json['success'], req.json assert list(notifications.load(api_authed)) req = application.delete_json('/api/02/notifications') assert req.status_int == 200 assert req.json['success'], req.json assert not list(notifications.load(api_authed))
def test_create(file): acct = Record.new() with file.open('rb') as file1, file.open('rb') as file2: gen = Image.new(acct, [file1, file2], allow_gif=file.suffix == '.gif') objects = list(gen) assert len(objects) # True with allow_gif = True assert len(objects) == 2 assert all(isinstance(x, Image) for x in objects) assert all(x.owner == acct for x in objects) assert all(x.good() for x in objects) if file.suffix == '.gif': with file.open('rb') as f: objects = list(Image.new(acct, [f], allow_gif=False)) assert not objects assert len(objects) == 0
def test_all(item_type): item = item_type.new() # Won't fail for Image try: item = next(item) except TypeError: pass account = Record.new() old_score = item.score ratings.up(account, item) assert item.score == old_score + 1 # No permission checking ratings.down(account, item) assert item.score == old_score - 1 ratings.un(account, item) assert item.score == old_score
def test_reset(): account = Record.new() with account: # Set the one we can actually check account.email = pytest.config.getoption("--send-to") for reset in ({"name": account.name}, {"email": account.email}, {"acct": account}): inst = auth.ResetNoKey(**reset) assert inst.good() if pytest.config.getoption("--no-send"): # set .key anyway inst._generate_key() else: with (consts.VIEWS / "email" / "password-reset.html").open() as tf: # No need to load environment inst.send(host="not_critical", template=Template(tf.read())) assert auth.Reset(inst.key).good() # Must not be able to reuse assert not auth.Reset(inst.key).good()
def test_set(file): # Test setcover and setavatar methods with file.open('rb') as f: obj = next(Image.new(Record.new(), [f], allow_gif=file.suffix == '.gif')) obj.setavatar() obj.setcover()
def test_create(): acct = Record.new() objects = next(Image.new()), post = posts.Post.new(acct, content='<string>', images=objects) assert post.good()
def api_authed(): acct = Record.new() for name, value in acct.cookies.items(): application.set_cookie(name, value) return acct
def test_signin(): acct = Record.new() assert not auth.signin(acct, "guaranteed to be incorrect") assert auth.signin(acct, acct.plaintext_pwd)
def test_downloading_bad_urls(url): acct = Record.new() bad = list(Image.new(acct, [url], allow_gif=True)) assert not bad # ♬ Not baaaaaad at a-a-a-all ♬