import json
import traceback
import unittest
from test import test_support
from keychain import PrivateKeychain, PublicKeychain
from pybitcoin import BitcoinPrivateKey, BitcoinPublicKey
from blockstack_profiles import sign_profile_tokens, get_profile_from_tokens, \
    create_zone_file


class TokeningTests(unittest.TestCase):
    def setUp(self):
        self.master_private_key = BitcoinPrivateKey()

    def tearDown(self):
        pass

    def test_basic_tokening(self):
        profile_components = [
            {"name": "Naval Ravikant"},
            {"birthDate": "1980-01-01"}
        ]
        reference_profile = {
            "name": "Naval Ravikant", 
            "birthDate": "1980-01-01"
        }
        # tokenize the profile
        profile_token_records = sign_profile_tokens(
            profile_components, self.master_private_key.to_hex())
        print json.dumps(profile_token_records, indent=2)
        self.assertTrue(isinstance(profile_token_records, list))
        # recover the profile
        profile = get_profile_from_tokens(
            profile_token_records, self.master_private_key.public_key().to_hex())
        print json.dumps(profile, indent=2)
        self.assertTrue(isinstance(profile, object))
        self.assertEqual(profile, reference_profile)


class ZonefileTests(unittest.TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_zone_file_creation(self):
        origin = "naval.id"
        token_file_url = "https://mq9.s3.amazonaws.com/naval.id/profile.json"
        zone_file = create_zone_file(origin, token_file_url)
        print json.dumps(zone_file, indent=2)
        self.assertTrue(isinstance(zone_file, dict))


def test_main():
    test_support.run_unittest(
        TokeningTests,
        ZonefileTests
    )


if __name__ == '__main__':
    test_main()

@@ -279,16 +279,48 @@
 one_file
+, get_person_from_legacy_format 
 %0A%0A%0Aclass
@@ -1740,16 +1740,1697 @@
 dict))%0A%0A
+    def test_profile_format_migration(self):%0A        legacy_profile = %7B%0A            %22data_record%22: %7B%0A                %22avatar%22: %7B%0A                    %22url%22: %22https://s3.amazonaws.com/kd4/judecn%22%0A                %7D,%0A                %22bio%22: %22PhD student%22,%0A                %22bitcoin%22: %7B%0A                    %22address%22: %2217zf596xPvV8Z8ThbWHZHYQZEURSwebsKE%22%0A                %7D,%0A                %22cover%22: %7B%0A                    %22url%22: %22https://s3.amazonaws.com/97p/gQZ.jpg%22%0A                %7D,%0A                %22facebook%22: %7B%0A                    %22proof%22: %7B%0A                        %22url%22: %22https://facebook.com/sunspider/posts/674912239245011%22%0A                    %7D,%0A                    %22username%22: %22sunspider%22%0A                %7D,%0A                %22github%22: %7B%0A                    %22proof%22: %7B%0A                        %22url%22: %22https://gist.github.com/jcnelson/70c02f80f8d4b0b8fc15%22%0A                    %7D,%0A                    %22username%22: %22jcnelson%22%0A                %7D,%0A                %22location%22: %7B%0A                    %22formatted%22: %22Princeton University%22%0A                %7D,%0A                %22name%22: %7B%0A                    %22formatted%22: %22Jude Nelson%22%0A                %7D,%0A                %22twitter%22: %7B%0A                    %22proof%22: %7B%0A                        %22url%22: %22https://twitter.com/judecnelson/status/507374756291555328%22%0A                    %7D,%0A                    %22username%22: %22judecnelson%22%0A                %7D,%0A                %22v%22: %220.2%22,%0A                %22website%22: %22http://www.cs.princeton.edu/~jcnelson%22%0A            %7D%0A        %7D%0A        zone_file = get_person_from_legacy_format( legacy_profile )%0A        print json.dumps(zone_file, indent=2, sort_keys=True)%0A        self.assertTrue(isinstance(zone_file, dict))%0A
 %0Adef tes

Add basic unit test for profile conversion