|
| 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)) |
0 commit comments