2022-12-24 16:34:24 +00:00
|
|
|
"""Unit tests for graph.py."""
|
2023-01-13 19:40:52 +00:00
|
|
|
from granary import bluesky
|
2022-12-29 01:30:22 +00:00
|
|
|
from oauth_dropins.webutil.testutil import requests_response
|
|
|
|
import requests
|
|
|
|
|
2023-01-19 15:00:13 +00:00
|
|
|
from .test_activitypub import ACTOR, FOLLOW, FOLLOW_WITH_ACTOR, FOLLOW_WITH_OBJECT
|
2022-12-29 01:30:22 +00:00
|
|
|
from . import testutil
|
2023-01-19 23:29:52 +00:00
|
|
|
from models import Follower, User
|
2022-12-29 01:30:22 +00:00
|
|
|
|
2023-01-13 19:40:52 +00:00
|
|
|
SUBJECT = {
|
2023-04-08 19:47:22 +00:00
|
|
|
'$type': 'app.bsky.actor.defs#profileView',
|
2023-03-19 22:43:55 +00:00
|
|
|
'did': 'did:web:user.com',
|
|
|
|
'handle': 'user.com',
|
2023-04-08 19:47:22 +00:00
|
|
|
'description': None,
|
2023-01-13 19:40:52 +00:00
|
|
|
}
|
|
|
|
FOLLOWERS_BSKY = [{
|
|
|
|
'$type': 'app.bsky.graph.getFollowers#follower',
|
|
|
|
'did': 'did:web:other',
|
2023-01-13 20:50:46 +00:00
|
|
|
'handle': 'yoozer@other',
|
2023-01-13 19:40:52 +00:00
|
|
|
'indexedAt': '2022-01-02T03:04:05+00:00',
|
2023-04-08 19:47:22 +00:00
|
|
|
'description': None,
|
2023-01-13 19:40:52 +00:00
|
|
|
}, {
|
|
|
|
'$type': 'app.bsky.graph.getFollowers#follower',
|
2023-03-19 22:43:55 +00:00
|
|
|
'did': 'did:web:mas.to:users:swentel',
|
|
|
|
'handle': 'mas.to/users/swentel',
|
2023-02-14 16:25:41 +00:00
|
|
|
'displayName': 'Mrs. ☕ Foo',
|
2023-03-19 22:43:55 +00:00
|
|
|
'avatar': 'https://user.com/me.jpg',
|
2023-01-13 19:40:52 +00:00
|
|
|
'indexedAt': '2022-01-02T03:04:05+00:00',
|
2023-04-08 19:47:22 +00:00
|
|
|
'description': None,
|
2023-01-13 19:40:52 +00:00
|
|
|
}]
|
2022-12-29 01:30:22 +00:00
|
|
|
|
2023-01-13 19:40:52 +00:00
|
|
|
|
|
|
|
class XrpcGraphTest(testutil.TestCase):
|
|
|
|
|
2023-02-18 22:59:51 +00:00
|
|
|
def test_getProfile_no_user(self):
|
2023-01-15 18:07:04 +00:00
|
|
|
resp = self.client.get('/xrpc/app.bsky.graph.getFollowers')
|
|
|
|
self.assertEqual(400, resp.status_code)
|
|
|
|
|
2023-02-18 22:59:51 +00:00
|
|
|
def test_getFollowers_not_domain(self):
|
2023-01-13 19:40:52 +00:00
|
|
|
resp = self.client.get('/xrpc/app.bsky.graph.getFollowers',
|
|
|
|
query_string={'user': 'not a domain'})
|
|
|
|
self.assertEqual(400, resp.status_code)
|
|
|
|
|
2023-02-18 22:59:51 +00:00
|
|
|
def test_getFollowers_no_user(self):
|
2023-01-19 23:29:52 +00:00
|
|
|
resp = self.client.get('/xrpc/app.bsky.graph.getFollowers',
|
|
|
|
query_string={'user': 'no.com'})
|
|
|
|
self.assertEqual(400, resp.status_code)
|
|
|
|
|
2023-02-18 22:59:51 +00:00
|
|
|
def test_getFollowers_empty(self):
|
2023-03-19 22:43:55 +00:00
|
|
|
self.make_user('user.com')
|
2023-01-19 23:29:52 +00:00
|
|
|
|
2023-01-13 19:40:52 +00:00
|
|
|
resp = self.client.get('/xrpc/app.bsky.graph.getFollowers',
|
2023-03-19 22:43:55 +00:00
|
|
|
query_string={'user': 'user.com'})
|
2023-01-13 19:40:52 +00:00
|
|
|
self.assertEqual(200, resp.status_code)
|
2023-04-08 19:47:22 +00:00
|
|
|
self.assertEqual({
|
2023-01-13 19:40:52 +00:00
|
|
|
'subject': SUBJECT,
|
|
|
|
'cursor': '',
|
|
|
|
'followers': [],
|
|
|
|
}, resp.json)
|
|
|
|
|
2023-02-18 22:59:51 +00:00
|
|
|
def test_getFollowers(self):
|
2023-03-19 22:43:55 +00:00
|
|
|
self.make_user('user.com')
|
2023-01-19 23:29:52 +00:00
|
|
|
|
2023-02-18 22:59:51 +00:00
|
|
|
other_follow = {
|
|
|
|
**FOLLOW,
|
|
|
|
'actor': {
|
2023-04-08 19:47:22 +00:00
|
|
|
'type': 'Person',
|
2023-02-18 22:59:51 +00:00
|
|
|
'url': 'http://other',
|
|
|
|
'preferredUsername': 'yoozer',
|
|
|
|
},
|
2023-01-19 15:00:13 +00:00
|
|
|
}
|
|
|
|
|
2023-03-19 22:43:55 +00:00
|
|
|
Follower.get_or_create('user.com', 'https://no/stored/follow')
|
|
|
|
Follower.get_or_create('user.com', 'https://masto/user',
|
2023-02-24 13:25:29 +00:00
|
|
|
last_follow=FOLLOW_WITH_ACTOR)
|
2023-03-19 22:43:55 +00:00
|
|
|
Follower.get_or_create('user.com', 'http://other',
|
2023-02-24 13:25:29 +00:00
|
|
|
last_follow=other_follow)
|
2023-01-13 20:50:46 +00:00
|
|
|
Follower.get_or_create('nope.com', 'http://nope',
|
2023-02-24 13:25:29 +00:00
|
|
|
last_follow=other_follow)
|
2023-01-13 19:40:52 +00:00
|
|
|
|
|
|
|
resp = self.client.get('/xrpc/app.bsky.graph.getFollowers',
|
2023-03-19 22:43:55 +00:00
|
|
|
query_string={'user': 'user.com'})
|
2023-01-13 19:40:52 +00:00
|
|
|
self.assertEqual(200, resp.status_code)
|
2023-04-08 19:47:22 +00:00
|
|
|
self.assertEqual({
|
2023-01-13 19:40:52 +00:00
|
|
|
'subject': SUBJECT,
|
|
|
|
'cursor': '',
|
|
|
|
'followers': FOLLOWERS_BSKY,
|
|
|
|
}, resp.json)
|
|
|
|
|
2023-02-18 22:59:51 +00:00
|
|
|
def test_getFollows_not_domain(self):
|
2023-01-13 20:50:46 +00:00
|
|
|
resp = self.client.get('/xrpc/app.bsky.graph.getFollows',
|
|
|
|
query_string={'user': 'not a domain'})
|
|
|
|
self.assertEqual(400, resp.status_code)
|
|
|
|
|
2023-02-18 22:59:51 +00:00
|
|
|
def test_getFollows_empty(self):
|
2023-03-19 22:43:55 +00:00
|
|
|
self.make_user('user.com')
|
2023-01-19 23:29:52 +00:00
|
|
|
|
2023-01-13 20:50:46 +00:00
|
|
|
resp = self.client.get('/xrpc/app.bsky.graph.getFollows',
|
2023-03-19 22:43:55 +00:00
|
|
|
query_string={'user': 'user.com'})
|
2023-01-13 20:50:46 +00:00
|
|
|
self.assertEqual(200, resp.status_code)
|
2023-04-08 19:47:22 +00:00
|
|
|
self.assertEqual({
|
2023-01-13 20:50:46 +00:00
|
|
|
'subject': SUBJECT,
|
|
|
|
'cursor': '',
|
|
|
|
'follows': [],
|
|
|
|
}, resp.json)
|
|
|
|
|
2023-02-18 22:59:51 +00:00
|
|
|
def test_getFollows(self):
|
2023-03-19 22:43:55 +00:00
|
|
|
self.make_user('user.com')
|
2023-01-19 23:29:52 +00:00
|
|
|
|
2023-02-18 22:59:51 +00:00
|
|
|
other_follow = {
|
|
|
|
**FOLLOW,
|
|
|
|
'object': {
|
2023-04-08 19:47:22 +00:00
|
|
|
'type': 'Person',
|
2023-02-18 22:59:51 +00:00
|
|
|
'url': 'http://other',
|
|
|
|
'preferredUsername': 'yoozer',
|
|
|
|
},
|
2023-01-19 15:00:13 +00:00
|
|
|
}
|
|
|
|
|
2023-03-19 22:43:55 +00:00
|
|
|
Follower.get_or_create('https://no/stored/follow', 'user.com')
|
|
|
|
Follower.get_or_create('https://masto/user', 'user.com',
|
2023-02-24 13:25:29 +00:00
|
|
|
last_follow=FOLLOW_WITH_OBJECT)
|
2023-03-19 22:43:55 +00:00
|
|
|
Follower.get_or_create( 'http://other', 'user.com',
|
2023-02-24 13:25:29 +00:00
|
|
|
last_follow=other_follow)
|
2023-01-13 20:50:46 +00:00
|
|
|
Follower.get_or_create('http://nope', 'nope.com',
|
2023-02-24 13:25:29 +00:00
|
|
|
last_follow=other_follow)
|
2023-01-13 20:50:46 +00:00
|
|
|
|
|
|
|
resp = self.client.get('/xrpc/app.bsky.graph.getFollows',
|
2023-03-19 22:43:55 +00:00
|
|
|
query_string={'user': 'user.com'})
|
2023-01-13 20:50:46 +00:00
|
|
|
self.assertEqual(200, resp.status_code)
|
2023-04-08 19:47:22 +00:00
|
|
|
self.assertEqual({
|
2023-01-13 20:50:46 +00:00
|
|
|
'subject': SUBJECT,
|
|
|
|
'cursor': '',
|
|
|
|
'follows': FOLLOWERS_BSKY,
|
|
|
|
}, resp.json)
|