#!/usr/bin/env python
#
#  redirect_tests.py:  Test ra_dav handling of server-side redirects
#
#  Subversion is a tool for revision control.
#  See http://subversion.apache.org for more information.
#
# ====================================================================
#    Licensed to the Apache Software Foundation (ASF) under one
#    or more contributor license agreements.  See the NOTICE file
#    distributed with this work for additional information
#    regarding copyright ownership.  The ASF licenses this file
#    to you under the Apache License, Version 2.0 (the
#    "License"); you may not use this file except in compliance
#    with the License.  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing,
#    software distributed under the License is distributed on an
#    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#    KIND, either express or implied.  See the License for the
#    specific language governing permissions and limitations
#    under the License.
######################################################################

# General modules
import os, re

# Our testing module
import svntest

# (abbreviations)
Skip = svntest.testcase.Skip
SkipUnless = svntest.testcase.SkipUnless
XFail = svntest.testcase.XFail
Wimp = svntest.testcase.Wimp

# Regular expression which matches the redirection notification
redirect_regex = re.compile(r"^Redirecting to URL '.*'")

# Generic UUID-matching regular expression
uuid_regex = re.compile(r"[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}")


def verify_url(wc_path, url, wc_path_is_file=False):
  # check that we have a Repository Root and Repository UUID
  name = os.path.basename(wc_path)
  expected = {'Path' : re.escape(wc_path),
              'URL' : url,
              'Repository Root' : '.*',
              'Revision' : '.*',
              'Node Kind' : 'directory',
              'Repository UUID' : uuid_regex,
             }
  if wc_path_is_file:
    expected.update({'Name' : name,
                     'Node Kind' : 'file',
                     })
  svntest.actions.run_and_verify_info([expected], wc_path)


######################################################################
# Tests
#
#   Each test must return on success or raise on failure.

#----------------------------------------------------------------------

def redirected_checkout(sbox):
  "redirected checkout"

  sbox.build(create_wc=False)
  wc_dir = sbox.add_wc_path("my")
  co_url = sbox.redirected_root_url()

  # Checkout the working copy via its redirect URL
  exit_code, out, err = svntest.main.run_svn(None, 'co', co_url, wc_dir)
  if err:
    raise svntest.Failure
  if not redirect_regex.match(out[0]):
    raise svntest.Failure

  # Verify that we have the expected URL.
  verify_url(wc_dir, sbox.repo_url)

#----------------------------------------------------------------------

def redirected_update(sbox):
  "redirected update"

  sbox.build()
  wc_dir = sbox.wc_dir
  relocate_url = sbox.redirected_root_url()

  # Relocate (by cheating) the working copy to the redirect URL.  When
  # we then update, we'll expect to find ourselves automagically back
  # to the original URL.  (This is because we can't easily introduce a
  # redirect to the Apache configuration from the test suite here.)
  svntest.actions.no_relocate_validation()
  exit_code, out, err = svntest.main.run_svn(None, 'sw', '--relocate',
                                             sbox.repo_url, relocate_url,
                                             wc_dir)
  svntest.actions.do_relocate_validation()

  # Now update the working copy.
  exit_code, out, err = svntest.main.run_svn(None, 'up', wc_dir)
  if err:
    raise svntest.Failure
  if not redirect_regex.match(out[0]):
    raise svntest.Failure

  # Verify that we have the expected URL.
  verify_url(wc_dir, sbox.repo_url)

#----------------------------------------------------------------------

########################################################################
# Run the tests

# list all tests here, starting with None:
test_list = [ None,
              SkipUnless(redirected_checkout,
                         svntest.main.is_ra_type_dav),
              SkipUnless(redirected_update,
                         svntest.main.is_ra_type_dav),
             ]

if __name__ == '__main__':
  svntest.main.run_tests(test_list)
  # NOTREACHED


### End of file.

@@ -2414,32 +2414,1091 @@
 --------------%0A%0A
+def temporary_redirect(sbox):%0A  %22temporary redirect should error out%22%0A%0A  sbox.build(create_wc=False)%0A  wc_dir = sbox.add_wc_path(%22my%22)%0A  co_url = sbox.redirected_root_url(temporary=True)%0A%0A  # Try various actions against the repository, expecting an error%0A  # that indicates that some relocation has occurred.%0A  exit_code, out, err = svntest.main.run_svn('.*moved temporarily.*',%0A                                             'info', co_url)%0A  exit_code, out, err = svntest.main.run_svn('.*moved temporarily.*',%0A                                             'co', co_url, wc_dir)%0A  exit_code, out, err = svntest.main.run_svn('.*moved temporarily.*',%0A                                             'mkdir', '-m', 'MKDIR',%0A                                             co_url + '/newdir')%0A  exit_code, out, err = svntest.main.run_svn('.*moved temporarily.*',%0A                                             'delete', '-m', 'DELETE',%0A                                             co_url + '/iota')%0A%0A#----------------------------------------------------------------------%0A%0A
 def redirected_c
@@ -5223,16 +5223,116 @@
 %5B None,%0A
+              SkipUnless(temporary_redirect,%0A                         svntest.main.is_ra_type_dav),%0A
         

Test that temporary redirects return the expected error.