Skip to content
This repository was archived by the owner on May 16, 2019. It is now read-only.

Commit 39e85a9

Browse files
committed
Merge pull request #220 from tomgalloway/master
Initial tests for market.protocol
2 parents ace9941 + 472344d commit 39e85a9

File tree

4 files changed

+244
-9
lines changed

4 files changed

+244
-9
lines changed

market/profile.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,29 @@ def update(self, user_info):
3737

3838
def add_social_account(self, account_type, username, proof=None):
3939
s = self.profile.SocialAccount()
40-
for social_account in self.profile.social:
41-
if social_account.type == s.SocialType.Value(account_type.upper()):
42-
self.profile.social.remove(social_account)
43-
s.type = s.SocialType.Value(account_type.upper())
44-
s.username = username
45-
if proof:
46-
s.proof_url = proof
47-
self.profile.social.extend([s])
40+
try:
41+
self._remove_social_if_found(account_type)
42+
s.type = s.SocialType.Value(account_type.upper())
43+
s.username = username
44+
if proof:
45+
s.proof_url = proof
46+
self.profile.social.extend([s])
47+
except ValueError:
48+
return
4849
self.db.profile.set_proto(self.profile.SerializeToString())
4950

5051
def remove_social_account(self, account_type):
52+
try:
53+
self._remove_social_if_found(account_type)
54+
except ValueError:
55+
return
56+
self.db.profile.set_proto(self.profile.SerializeToString())
57+
58+
def _remove_social_if_found(self, account_type):
5159
s = self.profile.SocialAccount()
60+
st = s.SocialType.Value(account_type.upper())
5261
for social_account in self.profile.social:
53-
if social_account.type == s.SocialType.Value(account_type.upper()):
62+
if social_account.type == st:
5463
self.profile.social.remove(social_account)
5564
self.db.profile.set_proto(self.profile.SerializeToString())
5665

market/protocol.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def rpc_get_image(self, sender, image_hash):
6161
self.router.addContact(sender)
6262
try:
6363
if len(image_hash) != 20:
64+
self.log.warning("Image hash is not 20 characters %s" % image_hash)
6465
raise Exception("Invalid image hash")
6566
self.log.info("serving image %s to %s" % (image_hash.encode('hex'), sender))
6667
with open(self.db.filemap.get_file(image_hash.encode("hex")), "rb") as filename:

market/tests/test_profile.py

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
from twisted.trial import unittest
2+
from twisted.python import log
3+
from protos import objects
4+
import os
5+
6+
from db.datastore import Database
7+
from market.profile import Profile
8+
9+
class MarketProfileTest(unittest.TestCase):
10+
PUBLIC_KEY = """-----BEGIN PGP PUBLIC KEY BLOCK-----
11+
Version: GnuPG v1
12+
13+
mQENBFbGMvQBCADI9MkIEyVeyeAV+R4za8DuFEJbUviWmwTV+iCpt4utcsBNQa+/
14+
MWxcQcZK76hY9l6/xvXNZifnMozfifFL4n+FR4hFYp8fwB6QjikACsd6CfO2coUk
15+
p3aZSvP1ops2Z/LRNLf1QAqtltnMqkBcKPHp3JMcZUmOx1gOWfSrnc2b2Jk45sW2
16+
pD0tk3v4UU/4WP20lzHnnCTQaVT7RkDivcZEVSfACtYWKBvL8iHtDJds0hsb5nSs
17+
qUTnK7IPkAdL8iHTNtCRoReyPPbnTQQMdWGNxyBpgOd2tAUtWEMyUfLa9/Hl0dgA
18+
TnI6c8SG8mugvucoeLujqJjlzfIVR8QejLd9ABEBAAG0HE9CIFRlc3QgPG9idGVz
19+
dEBpbnZhbGlkLmNvbT6JATgEEwECACIFAlbGMvQCGwMGCwkIBwMCBhUIAgkKCwQW
20+
AgMBAh4BAheAAAoJEIsTPki+1mH2NtsH/jprOKjdltcUS4eQ3PD555xYX++qpVkT
21+
lv0MH+NP3eTD8v/LA1pAqjI1Lb58SxT7j5mAgJkJmMZylmHhptOBvtW8hpIAAdYM
22+
4ywDVyZO9lz9tk4snU8cZiZqKi3Rp3FWwR3dR+Eyx1ciiYNN2nhQoLZUhynE2ShB
23+
O6dk9VpWjdNhK1PU2umSopNJBb6bTes00HnHordml/StrxQPmyoJ8ZEV452Mztzx
24+
WcryI42F7OsHPAg3hxD1QZ9cf9SIl9/p4x8Td/yms2y+rL8p7t4zBmgxAFDcBRsh
25+
qllYKJpeVP4Jb6uOGiVyUUB+rr5/G8C9tpjPjYpbe0EHvTOdKqSZrMe5AQ0EVsYy
26+
9AEIAJci80lDNCrpQaOIgXBouWhOwKMzteg/LvtwVbtLu8biJSNv1z7v/BBBhYuJ
27+
OzbF60L4a/e25dsgEc/3kG2FzBAjq3ecR/2DxfF4e2dio+SUtOdf9ycK3VXmJMas
28+
KpYmOdSAfMMSzDmCBbwXQ27gxBdu4+gSmsWh31MLACZ29C0L+p99x6uoFX5hgOC8
29+
XelhWAa4dsCOXDIbkOTVl44XSoMyQtFZmEsyRp7/7h78osgssinFgY87/EXE3exC
30+
fYNLZhotQWuqKn+2NvvED/UShVfzOCmkobVltAKRVC8vgQn3ALBgmtw7J9P0fr2z
31+
IW0E9NR6I6JdbWLz+6hZ6sTSFZ0AEQEAAYkBHwQYAQIACQUCVsYy9AIbDAAKCRCL
32+
Ez5IvtZh9jsJB/4z9sNqCiKo/FYIBG6mTlWTZ0fTnfTy81zUTVmfPhJtnjGDGk6l
33+
r5jqi6itSx+qkRiV9W9IVUQLXboKIXFjugiWNTqbYtURiCigNis2SEkQ1uYp3y3H
34+
o7IQG52dTqB5gopOx/jNDrcv0z7Vf71UOg9L0WU/5F4AZhxUMo0jCQudWbp0okEo
35+
yOjZDvyBt/8ESAZCa/51doYeKBq+olS03AusUF6S1tyIOHaFWA/cCumc0QzdrHkD
36+
gPJjjEdv9iS9Gyyc/G5p9xcIch3lw+pcRLqBAch4AGKJPudEN9P/fbbMCeCbJLif
37+
FAfk98lyZjL/oXBb5qX9qcQguehRyuE4ccGD=F9sf
38+
-----END PGP PUBLIC KEY BLOCK-----"""
39+
40+
SIGNATURE = """-----BEGIN PGP SIGNED MESSAGE-----
41+
Hash: SHA1
42+
43+
4c103483-3757-4ac6-85af-51e1f3193236
44+
-----BEGIN PGP SIGNATURE-----
45+
Version: GnuPG v1
46+
47+
iQEcBAEBAgAGBQJWxjoAAAoJEIsTPki+1mH2cGkH/0w+Q0V00oYtM86kifiH5iY7
48+
//uMjCv0pAaupcanJMBi0lJPVa0uijz09fXThF+pWq85nDTx5FDklNexzzqUEaaR
49+
Qggk3abEm9LjXYS9gq0AR5Va0qxBo4Xe8oMAheYIyC4/ikG627ew+x5VMPfKfbFq
50+
zaX5wLzZGfrFtWetE1kFBbzlmdb8jARlkRB68nvzSH3vCoyLkRXa9/l7FXouIuNG
51+
4i5iTMeM4T+bu6A5yS4Fz67+AGZnC2VZwzsz3RrpuAV65LoM3w2wfFrM6sni76Gu
52+
/+VUFmAv+o3i0gbTCbaf3wc0SYrD5hrzjilqPVCmTxQYe4cOD8N6D1ZrTYyMdYU=
53+
=FPwF
54+
-----END PGP SIGNATURE-----"""
55+
VALID_GUID = '4c103483-3757-4ac6-85af-51e1f3193236'
56+
57+
def setUp(self):
58+
self.catcher = []
59+
observer = self.catcher.append
60+
log.addObserver(observer)
61+
self.addCleanup(log.removeObserver, observer)
62+
self.db = Database(filepath="test.db")
63+
self.createTestUser()
64+
65+
def createTestUser(self):
66+
u = objects.Profile()
67+
u.name = "test_name"
68+
u.location = 2
69+
u.about = "hello world"
70+
s = u.SocialAccount()
71+
s.username = "test_fb_username"
72+
s.type = s.SocialType.Value("FACEBOOK")
73+
u.social.extend([s])
74+
self.db.profile.set_proto(u.SerializeToString())
75+
self.db.profile.set_temp_handle("test_handle")
76+
77+
def tearDown(self):
78+
os.remove("test.db")
79+
80+
def test_MarketProfile_get_success(self):
81+
p = Profile(self.db).get()
82+
self.assertEqual('test_name', p.name)
83+
self.assertEqual(2, p.location)
84+
self.assertEqual('hello world', p.about)
85+
self.assertEqual(1, len(p.social))
86+
self.assertEqual(0, p.social[0].type)
87+
self.assertEqual('test_fb_username', p.social[0].username)
88+
89+
def test_MarketProtocol_get_serialized_success(self):
90+
p = Profile(self.db).get(serialized=True)
91+
self.assertEqual("\n\ttest_name\x10\x02R\x0bhello worldr\x12\x12\x10test_fb_username", p)
92+
93+
def test_MarketProfile_remove_field_success(self):
94+
p = Profile(self.db)
95+
p.remove_field("about")
96+
user = p.get()
97+
self.assertEqual('test_name', user.name)
98+
self.assertEqual('', user.about)
99+
100+
def test_MarketProfile_remove_social(self):
101+
p = Profile(self.db)
102+
p.remove_social_account("FACEBOOK")
103+
u = p.get()
104+
self.assertEqual(0, len(u.social))
105+
106+
def test_MarketProfile_remove_lowercase_social(self):
107+
p = Profile(self.db)
108+
p.remove_social_account("facebook")
109+
u = p.get()
110+
self.assertEqual(0, len(u.social))
111+
112+
def test_MarketProfile_remove_social_invalid(self):
113+
p = Profile(self.db)
114+
p.remove_social_account("TEST")
115+
u = p.get()
116+
self.assertEqual(1, len(u.social))
117+
118+
def test_MarketProfile_add_social_no_proof(self):
119+
p = Profile(self.db)
120+
p.add_social_account("TWITTER", "test_twitter_username")
121+
u = p.get()
122+
self.assertEqual(2, len(u.social))
123+
self.assertEqual(0, u.social[0].type)
124+
self.assertEqual('test_fb_username', u.social[0].username)
125+
self.assertEqual(1, u.social[1].type)
126+
self.assertEqual('test_twitter_username', u.social[1].username)
127+
128+
def test_MarketProfile_replace_social_no_proof(self):
129+
p = Profile(self.db)
130+
p.add_social_account("FACEBOOK", "test_updated_username")
131+
u = p.get()
132+
self.assertEqual(1, len(u.social))
133+
self.assertEqual(0, u.social[0].type)
134+
self.assertEqual('test_updated_username', u.social[0].username)
135+
136+
def test_MarketProfile_add_social_with_proof(self):
137+
p = Profile(self.db)
138+
p.add_social_account("TWITTER", "test_twitter_username", "http://test_url")
139+
u = p.get()
140+
self.assertEqual(2, len(u.social))
141+
self.assertEqual(0, u.social[0].type)
142+
self.assertEqual('test_fb_username', u.social[0].username)
143+
self.assertEqual('', u.social[0].proof_url)
144+
self.assertEqual(1, u.social[1].type)
145+
self.assertEqual('test_twitter_username', u.social[1].username)
146+
self.assertEqual('http://test_url', u.social[1].proof_url)
147+
148+
def test_MarketProfile_replace_social_with_proof(self):
149+
p = Profile(self.db)
150+
p.add_social_account("FACEBOOK", "test_updated_username", "http://fb_url")
151+
u = p.get()
152+
self.assertEqual(1, len(u.social))
153+
self.assertEqual(0, u.social[0].type)
154+
self.assertEqual('test_updated_username', u.social[0].username)
155+
self.assertEqual('http://fb_url', u.social[0].proof_url)
156+
157+
def test_MarketProfile_add_social_invalid(self):
158+
p = Profile(self.db)
159+
p.add_social_account("TEST", "test_twitter_username")
160+
u = p.get()
161+
self.assertEqual(1, len(u.social))
162+
self.assertEqual(0, u.social[0].type)
163+
self.assertEqual('test_fb_username', u.social[0].username)
164+
165+
def test_MarketProfile_update_success(self):
166+
u = objects.Profile()
167+
u.about = "updated world"
168+
p = Profile(self.db)
169+
p.update(u)
170+
updated_user = p.get()
171+
self.assertEqual("updated world", updated_user.about)
172+
173+
def test_MarketProfile_get_temp_handle(self):
174+
p = Profile(self.db)
175+
self.assertEqual("test_handle", p.get_temp_handle())
176+
177+
def test_MarketProfile_add_pgp_key_success(self):
178+
p = Profile(self.db)
179+
self.assertTrue(p.add_pgp_key(self.PUBLIC_KEY, self.SIGNATURE, self.VALID_GUID))
180+
u = p.get()
181+
self.assertEqual(self.SIGNATURE, u.pgp_key.signature)
182+
self.assertEqual(self.PUBLIC_KEY, u.pgp_key.public_key)
183+
184+
def test_MarketProfile_add_pgp_key_wrong_guid(self):
185+
p = Profile(self.db)
186+
wrong_guid = '5c2dedbd-5977-4326-b965-c9a2435c8e91'
187+
self.assertFalse(p.add_pgp_key(self.PUBLIC_KEY, self.SIGNATURE, wrong_guid))

market/tests/test_protocol.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from twisted.trial import unittest
2+
from twisted.python import log
3+
4+
from dht.node import Node
5+
from dht.utils import digest
6+
from dht.routing import RoutingTable
7+
from market.protocol import MarketProtocol
8+
from dht.tests.utils import mknode
9+
10+
class MarketProtocolTest(unittest.TestCase):
11+
def setUp(self):
12+
self.catcher = []
13+
observer = self.catcher.append
14+
log.addObserver(observer)
15+
self.addCleanup(log.removeObserver, observer)
16+
self.node = Node(digest("test"), "127.0.0.1", 1234)
17+
self.router = RoutingTable(self, 20, self.node.id)
18+
19+
def test_MarketProtocol_connect_multiplexer_correctly(self):
20+
mp = MarketProtocol(0, 0, 0, 0)
21+
self.assertEqual(mp.multiplexer, None)
22+
mp.connect_multiplexer("3")
23+
self.assertEqual(mp.multiplexer, "3")
24+
25+
def test_MarketProtocol_add_listener_correctly(self):
26+
mp = MarketProtocol(0, 0, 0, 0)
27+
self.assertEqual(len(mp.listeners), 0)
28+
mp.add_listener(3)
29+
self.assertEqual(len(mp.listeners), 1)
30+
31+
def test_MarketProtocol_rpc_get_image_invalid_image_hash(self):
32+
catcher = self.catcher
33+
mp = MarketProtocol(self.node, self.router, 0, 0)
34+
self.assertEqual(None, mp.rpc_get_image(mknode(), "invalid_hash"))
35+
catch_exception = catcher.pop()
36+
exception_message = catcher.pop()
37+
self.assertEquals(catch_exception["message"][0], "[WARNING] could not find image 696e76616c69645f68617368")
38+
self.assertEquals(exception_message["message"][0], "[WARNING] Image hash is not 20 characters invalid_hash")

0 commit comments

Comments
 (0)