def test_create_and_store_my_did__with_seed(self):
     conn = WalletConnection(self.WALLET_UID, self.WALLET_PASS_PHRASE)
     wallet = Wallet.objects.create(uid=self.WALLET_UID, owner=self.account)
     url = self.live_server_url + '/agent/admin/wallets/%s/did/create_and_store_my_did/' % wallet.uid
     # first: create wallet
     run_async(conn.create())
     try:
         cred = dict(pass_phrase=self.WALLET_PASS_PHRASE,
                     seed='000000000000000000000000Steward1')
         # open
         url_open = self.live_server_url + reverse(
             'admin-wallets-open', kwargs=dict(uid=self.WALLET_UID))
         resp = requests.post(url_open,
                              json=cred,
                              auth=HTTPBasicAuth(self.IDENTITY, self.PASS))
         self.assertEqual(200, resp.status_code)
         # FIRE!!!
         resp = requests.post(url,
                              json=cred,
                              auth=HTTPBasicAuth(self.IDENTITY, self.PASS))
         self.assertEqual(201, resp.status_code)
         info = resp.json()
         self.assertTrue(info.get('did'))
         self.assertTrue(info.get('verkey'))
         # Fire second time
         resp = requests.post(url,
                              json=cred,
                              auth=HTTPBasicAuth(self.IDENTITY, self.PASS))
         self.assertIn(resp.status_code, [201, 409])
     finally:
         os.popen("pkill -f run_wallet_agent")
         sleep(1)
         run_async(conn.delete())
 def test_create_invitation_with_did(self):
     conn = WalletConnection(self.WALLET_UID, self.WALLET_PASS_PHRASE)
     wallet = Wallet.objects.create(uid=self.WALLET_UID, owner=self.account)
     endpoint = Endpoint.objects.create(uid='endpoint_uid',
                                        owner=self.account,
                                        wallet=wallet,
                                        url='http://example.com/endpoint')
     # first: create wallet
     run_async(conn.create())
     run_async(conn.open())
     my_did, my_verkey = run_async(conn.create_and_store_my_did())
     run_async(conn.close())
     try:
         cred = dict(pass_phrase=self.WALLET_PASS_PHRASE, my_did=my_did)
         url = self.live_server_url + '/agent/admin/wallets/%s/endpoints/%s/invitations/' % (
             self.WALLET_UID, endpoint.uid)
         resp = requests.post(url,
                              json=cred,
                              auth=HTTPBasicAuth(self.IDENTITY, self.PASS))
         self.assertEqual(201, resp.status_code)
         instance = Invitation.objects.get(endpoint=endpoint)
         self.assertEqual(my_did, instance.my_did)
         resp = requests.post(url,
                              json=cred,
                              auth=HTTPBasicAuth(self.IDENTITY, self.PASS))
         self.assertEqual(201, resp.status_code)
     finally:
         os.popen("pkill -f run_wallet_agent")
         sleep(1)
         run_async(conn.delete())
 def test_list_my_dids_with_meta(self):
     conn = WalletConnection(self.WALLET_UID, self.WALLET_PASS_PHRASE)
     wallet = Wallet.objects.create(uid=self.WALLET_UID, owner=self.account)
     url = self.live_server_url + '/agent/admin/wallets/%s/did/list_my_dids_with_meta/' % wallet.uid
     # first: create wallet
     run_async(conn.create())
     try:
         # create did from seed
         run_async(conn.open())
         did, verkey = run_async(
             conn.create_and_store_my_did(
                 seed='000000000000000000000000Steward1'))
         run_async(conn.close())
         cred = dict(pass_phrase=self.WALLET_PASS_PHRASE)
         # open
         url_open = self.live_server_url + reverse(
             'admin-wallets-open', kwargs=dict(uid=self.WALLET_UID))
         resp = requests.post(url_open,
                              json=cred,
                              auth=HTTPBasicAuth(self.IDENTITY, self.PASS))
         self.assertEqual(200, resp.status_code)
         # FIRE!!!
         resp = requests.post(url,
                              json=cred,
                              auth=HTTPBasicAuth(self.IDENTITY, self.PASS))
         self.assertEqual(200, resp.status_code)
         raw = json.dumps(resp.json())
         self.assertIn(did, raw)
         self.assertIn(verkey, raw)
     finally:
         os.popen("pkill -f run_wallet_agent")
         sleep(1)
         run_async(conn.delete())
 def test_create_invitation_with_seed(self):
     conn = WalletConnection(self.WALLET_UID, self.WALLET_PASS_PHRASE)
     wallet = Wallet.objects.create(uid=self.WALLET_UID, owner=self.account)
     endpoint = Endpoint.objects.create(uid='endpoint_uid',
                                        owner=self.account,
                                        wallet=wallet,
                                        url='http://example.com/endpoint')
     # first: create wallet
     run_async(conn.create())
     try:
         seed = 'blablabla-seed-'
         expected_key = '3XvPjB4EDpmBBF4sRmqVbrQXQY5vk7zjiggSCGxSkPpV'
         cred = dict(pass_phrase=self.WALLET_PASS_PHRASE, seed=seed)
         url = self.live_server_url + '/agent/admin/wallets/%s/endpoints/%s/invitations/' % (
             self.WALLET_UID, endpoint.uid)
         resp = requests.post(url,
                              json=cred,
                              auth=HTTPBasicAuth(self.IDENTITY, self.PASS))
         self.assertEqual(201, resp.status_code)
         instance = Invitation.objects.get(endpoint=endpoint)
         connection_key = resp.json()['connection_key']
         self.assertEqual(expected_key, connection_key)
         self.assertEqual(instance.connection_key, connection_key)
         self.assertEqual(instance.seed, seed)
         resp = requests.post(url,
                              json=cred,
                              auth=HTTPBasicAuth(self.IDENTITY, self.PASS))
         self.assertEqual(409, resp.status_code)
     finally:
         os.popen("pkill -f run_wallet_agent")
         sleep(1)
         run_async(conn.delete())
 def test_create_invitation__with_label(self):
     conn = WalletConnection(self.WALLET_UID, self.WALLET_PASS_PHRASE)
     wallet = Wallet.objects.create(uid=self.WALLET_UID, owner=self.account)
     endpoint = Endpoint.objects.create(uid='endpoint_uid',
                                        owner=self.account,
                                        wallet=wallet,
                                        url='http://example.com/endpoint')
     # first: create wallet
     run_async(conn.create())
     try:
         label = 'My Test Label'
         cred = dict(pass_phrase=self.WALLET_PASS_PHRASE, label=label)
         url = self.live_server_url + '/agent/admin/wallets/%s/endpoints/%s/invitations/' % (
             self.WALLET_UID, endpoint.uid)
         resp = requests.post(url,
                              json=cred,
                              auth=HTTPBasicAuth(self.IDENTITY, self.PASS))
         self.assertEqual(201, resp.status_code)
         entity = resp.json()
         self.assertTrue(entity['url'])
         invite_url = entity['url']
         matches = re.match("(.+)?c_i=(.+)", invite_url)
         self.assertTrue(matches)
         _ = base64.urlsafe_b64decode(matches.group(2)).decode('utf-8')
         invite_msg = json.loads(_)
         self.assertEqual(label, invite_msg.get('label'))
     finally:
         os.popen("pkill -f run_wallet_agent")
         sleep(1)
         run_async(conn.delete())
 def test_open_close__via_http_header(self):
     conn = WalletConnection(self.WALLET_UID, self.WALLET_PASS_PHRASE)
     Wallet.objects.create(uid=self.WALLET_UID, owner=self.account)
     # first: create wallet
     run_async(conn.create())
     headers = dict()
     headers[HEADER_PASS_PHRASE] = self.WALLET_PASS_PHRASE
     try:
         # open
         url = self.live_server_url + reverse(
             'admin-wallets-open', kwargs=dict(uid=self.WALLET_UID))
         resp = requests.post(url,
                              auth=HTTPBasicAuth(self.IDENTITY, self.PASS),
                              headers=headers)
         self.assertEqual(200, resp.status_code)
         # is_open
         url = self.live_server_url + reverse(
             'admin-wallets-is-open', kwargs=dict(uid=self.WALLET_UID))
         resp = requests.get(url,
                             auth=HTTPBasicAuth(self.IDENTITY, self.PASS))
         self.assertEqual(200, resp.status_code)
         stat = resp.json()
         self.assertTrue(stat['is_open'])
         # close
         url = self.live_server_url + reverse(
             'admin-wallets-close', kwargs=dict(uid=self.WALLET_UID))
         resp = requests.post(url,
                              auth=HTTPBasicAuth(self.IDENTITY, self.PASS),
                              headers=headers)
         self.assertEqual(200, resp.status_code)
         # is_open
         url = self.live_server_url + reverse(
             'admin-wallets-is-open', kwargs=dict(uid=self.WALLET_UID))
         resp = requests.get(url,
                             auth=HTTPBasicAuth(self.IDENTITY, self.PASS))
         self.assertEqual(200, resp.status_code)
         stat = resp.json()
         self.assertFalse(stat['is_open'])
     finally:
         os.popen("pkill -f run_wallet_agent")
         sleep(1)
         run_async(conn.delete())
 def test_create_invitation(self):
     conn = WalletConnection(self.WALLET_UID, self.WALLET_PASS_PHRASE)
     wallet = Wallet.objects.create(uid=self.WALLET_UID, owner=self.account)
     endpoint = Endpoint.objects.create(uid='endpoint_uid',
                                        owner=self.account,
                                        wallet=wallet,
                                        url='http://example.com/endpoint')
     # first: create wallet
     run_async(conn.create())
     try:
         cred = dict(pass_phrase=self.WALLET_PASS_PHRASE)
         url = self.live_server_url + '/agent/admin/wallets/%s/endpoints/%s/invitations/' % (
             self.WALLET_UID, endpoint.uid)
         resp = requests.post(url,
                              json=cred,
                              auth=HTTPBasicAuth(self.IDENTITY, self.PASS))
         self.assertEqual(201, resp.status_code)
         instance = Invitation.objects.get(endpoint=endpoint)
         entity = resp.json()
         self.assertTrue(entity['url'])
         invite_url = entity['url']
         self.assertTrue(resp.json()['connection_key'])
         connection_key = resp.json()['connection_key']
         self.assertIn(instance.invitation_string, invite_url)
         self.assertIn(settings.INDY['INVITATION_URL_BASE'], invite_url)
         self.assertEqual(1, invite_url.count('c_i='))
         # check list
         resp = requests.get(url,
                             auth=HTTPBasicAuth(self.IDENTITY, self.PASS))
         self.assertEqual(200, resp.status_code)
         raw = str(resp.json())
         self.assertIn(invite_url, raw)
         self.assertIn(connection_key, raw)
     finally:
         os.popen("pkill -f run_wallet_agent")
         sleep(1)
         run_async(conn.delete())
    def test_invitation_ensure_exists(self):
        conn = WalletConnection(self.WALLET_UID, self.WALLET_PASS_PHRASE)
        wallet = Wallet.objects.create(uid=self.WALLET_UID, owner=self.account)
        endpoint = Endpoint.objects.create(uid='endpoint_uid',
                                           owner=self.account,
                                           wallet=wallet,
                                           url='http://example.com/endpoint')
        # first: create wallet
        run_async(conn.create())
        run_async(conn.open())
        my_did, my_verkey = run_async(conn.create_and_store_my_did())
        run_async(conn.close())
        try:
            seed = 'blablabla-seed-'
            expected_key = '3XvPjB4EDpmBBF4sRmqVbrQXQY5vk7zjiggSCGxSkPpV'
            cred = dict(pass_phrase=self.WALLET_PASS_PHRASE, seed=seed)
            url = self.live_server_url + '/agent/admin/wallets/%s/endpoints/%s/invitations/ensure_exists/' % (
                self.WALLET_UID, endpoint.uid)
            resp = requests.post(url,
                                 json=cred,
                                 auth=HTTPBasicAuth(self.IDENTITY, self.PASS))
            self.assertEqual(200, resp.status_code)
            i1 = resp.json()

            instance = Invitation.objects.get(endpoint=endpoint)
            self.assertEqual(seed, instance.seed)
            self.assertEqual(expected_key, instance.connection_key)
            self.assertIsNone(instance.my_did)
            resp = requests.post(url,
                                 json=cred,
                                 auth=HTTPBasicAuth(self.IDENTITY, self.PASS))
            self.assertEqual(200, resp.status_code)
            i2 = resp.json()

            self.assertEqual(
                1,
                Invitation.objects.filter(connection_key=expected_key).count())

            matches = re.match("(.+)?c_i=(.+)", i1['url'])
            invite_msg1 = Serializer.deserialize(
                base64.urlsafe_b64decode(
                    matches.group(2)).decode('utf-8')).to_dict()
            del invite_msg1['@id']
            matches = re.match("(.+)?c_i=(.+)", i2['url'])
            invite_msg2 = Serializer.deserialize(
                base64.urlsafe_b64decode(
                    matches.group(2)).decode('utf-8')).to_dict()
            del invite_msg2['@id']
            self.assertDictEqual(invite_msg1, invite_msg2)

            cred['my_did'] = my_did
            resp = requests.post(url,
                                 json=cred,
                                 auth=HTTPBasicAuth(self.IDENTITY, self.PASS))
            self.assertEqual(200, resp.status_code)
            instance = Invitation.objects.get(endpoint=endpoint)
            self.assertEqual(my_did, instance.my_did)

            self.assertEqual(1, Invitation.objects.count())
            cred = dict(pass_phrase=self.WALLET_PASS_PHRASE,
                        seed=seed + 'salt')
            resp = requests.post(url,
                                 json=cred,
                                 auth=HTTPBasicAuth(self.IDENTITY, self.PASS))
            self.assertEqual(200, resp.status_code)
            self.assertEqual(2, Invitation.objects.count())
        finally:
            os.popen("pkill -f run_wallet_agent")
            sleep(1)
            run_async(conn.delete())