From 035f7cf55d8e22bbc7ffac8ea3e07c215a6b0632 Mon Sep 17 00:00:00 2001
From: David Stainton <dstainton415@gmail.com>
Date: Tue, 23 Jun 2015 16:57:51 -0700
Subject: [PATCH] Teach magic-folder cli tests to test join results

- test if the client's dmd's readonly cap is present in the collective
readonly cap
---
 src/allmydata/test/test_cli_magic_folder.py | 51 ++++++++++++++++++---
 1 file changed, 45 insertions(+), 6 deletions(-)

diff --git a/src/allmydata/test/test_cli_magic_folder.py b/src/allmydata/test/test_cli_magic_folder.py
index 3dab5652..89664472 100644
--- a/src/allmydata/test/test_cli_magic_folder.py
+++ b/src/allmydata/test/test_cli_magic_folder.py
@@ -1,6 +1,7 @@
 import os.path
 import urllib
 import re
+import json
 
 from twisted.trial import unittest
 
@@ -10,7 +11,7 @@ from allmydata.scripts import cli, runner
 from allmydata.test.no_network import GridTestMixin
 from allmydata.util.encodingutil import quote_output, get_io_encoding
 from .test_cli import CLITestMixin
-
+from allmydata.scripts import magic_folder_cli
 
 class CreateMagicFolder(GridTestMixin, CLITestMixin, unittest.TestCase):
 
@@ -34,11 +35,42 @@ class CreateMagicFolder(GridTestMixin, CLITestMixin, unittest.TestCase):
         d.addCallback(_done)
         return d
 
+    def _diminish(self, write_cap):
+        d = self.do_cli("ls", "--json", write_cap)
+        def get_readonly_cap((rc,stdout,stderr)):
+            self.failUnless(rc == 0)
+            readonly_cap = json.loads(stdout)[1][u"ro_uri"]
+            return readonly_cap
+        d.addCallback(get_readonly_cap)
+        return d
+
+    def _try_joined_config(self, result):
+        collective_readonly_cap = fileutil.read(os.path.join(self.get_clientdir(), "private/collective_dircap"))
+        d = self.do_cli("ls", "--json", collective_readonly_cap)
+        def _done((rc,stdout,stderr)):
+            self.failUnless(rc == 0)
+            return (rc,stdout,stderr)
+        d.addCallback(_done)
+        def test_joined_magic_folder((rc,stdout,stderr)):
+            d2 = self._diminish(self.dmd_write_cap)
+            def fail_unless_dmd_readonly_exists(readonly_cap):
+                s = re.search(readonly_cap, stdout)
+                self.failUnless(s is not None)
+            d2.addCallback(fail_unless_dmd_readonly_exists)
+            return d2
+        d.addCallback(test_joined_magic_folder)
+        return d
+
+    def _get_caps_from_files(self, result):
+        self.magic_readonly_cap = fileutil.read(os.path.join(self.get_clientdir(), "private/collective_dircap"))
+        self.dmd_write_cap = fileutil.read(os.path.join(self.get_clientdir(), "private/magic_folder_dircap"))
+        self.failIf(self.magic_readonly_cap is None or self.dmd_write_cap is None)
+
     def _join(self, result):
         invite_code = result[1].strip()
-        d = self.do_cli("magic-folder", "join", invite_code, u"Alice_local_magic")
+        self.magic_readonly_cap, self.dmd_write_cap = invite_code.split(magic_folder_cli.INVITE_SEPERATOR)
+        d = self.do_cli("magic-folder", "join", invite_code, self.magic_local_dir)
         def _done((rc,stdout,stderr)):
-            print "_join rc %s" % (rc,)
             self.failUnless(rc == 0)
             return (rc,stdout,stderr)
         d.addCallback(_done)
@@ -46,25 +78,32 @@ class CreateMagicFolder(GridTestMixin, CLITestMixin, unittest.TestCase):
 
     def _check_config(self, result):
         client_config = fileutil.read(os.path.join(self.get_clientdir(), "tahoe.cfg"))
-        ret = re.search(r'\[magic_folder\]\nenabled = True\nlocal.directory = Alice_local_magic', client_config)
+        ret = re.search("\[magic_folder\]\nenabled = True\nlocal.directory = %s" % (self.magic_local_dir,), client_config)
         self.failIf(ret is None)
+        return result
 
     def test_create_and_then_invite_join(self):
         self.basedir = "cli/MagicFolder/create-and-then-invite-join"
         self.set_up_grid()
+        self.magic_local_dir = os.path.join(self.basedir, "magic")
+
         d = self._create_magic_folder()
         d.addCallback(self._invite)
         d.addCallback(self._join)
+        d.addCallback(self._try_joined_config)
         d.addCallback(self._check_config)
         return d
 
     def test_create_invite_join(self):
         self.basedir = "cli/MagicFolder/create-invite-join"
         self.set_up_grid()
-        magic_local_dir = os.path.join(self.basedir, "magic")
-        d = self.do_cli("magic-folder", "create", u"magic", u"Alice", magic_local_dir)
+        self.magic_local_dir = os.path.join(self.basedir, "magic")
+        d = self.do_cli("magic-folder", "create", u"magic", u"Alice", self.magic_local_dir)
         def _done((rc,stdout,stderr)):
             self.failUnless(rc == 0)
             return (rc,stdout,stderr)
         d.addCallback(_done)
+        d.addCallback(self._get_caps_from_files)
+        d.addCallback(self._try_joined_config)
+        d.addCallback(self._check_config)
         return d
-- 
2.45.2