<commit_before>#!/usr/bin/python
# Copyright (c) 2010 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import os

import pyauto_functional  # Must be imported before pyauto
import pyauto

class CookiesTest(pyauto.PyUITest):
  """Tests for Cookies."""

  def _ClearCookiesAndCheck(self, url):
    self.ClearBrowsingData(['COOKIES'], 'EVERYTHING')
    cookie_data = self.GetCookie(pyauto.GURL(url))
    self.assertEqual(0, len(cookie_data))

  def _CookieCheckRegularWindow(self, url):
    """Check the cookie for the given URL in a regular window."""
    self._ClearCookiesAndCheck(url)
    # Assert that the cookie data isn't empty after navigating to the url.
    self.NavigateToURL(url)
    cookie_data = self.GetCookie(pyauto.GURL(url))
    self.assertNotEqual(0, len(cookie_data))
    # Restart the browser and ensure the cookie data is the same.
    self.RestartBrowser(clear_profile=False)
    self.assertEqual(cookie_data, self.GetCookie(pyauto.GURL(url)))

  def _CookieCheckIncognitoWindow(self, url):
    """Check the cookie for the given URL in an incognito window."""
    self._ClearCookiesAndCheck(url)
    # Navigate to the URL in an incognito window and verify no cookie is set.
    self.RunCommand(pyauto.IDC_NEW_INCOGNITO_WINDOW)
    self.NavigateToURL(url, 1, 0)
    cookie_data = self.GetCookie(pyauto.GURL(url))
    self.assertEqual(0, len(cookie_data))

  def testSetCookies(self):
    """Test setting cookies and getting the value."""
    cookie_url = pyauto.GURL(self.GetFileURLForPath(
        os.path.join(self.DataDir(), 'title1.html')))
    cookie_val = 'foo=bar'
    self.SetCookie(cookie_url, cookie_val)
    self.assertEqual(cookie_val, self.GetCookie(cookie_url))

  def testCookiesHttp(self):
    """Test cookies set over HTTP for incognito and regular windows."""
    http_url = 'http://www.google.com'
    self._CookieCheckRegularWindow(http_url)
    self._CookieCheckIncognitoWindow(http_url)

  def testCookiesHttps(self):
    """Test cookies set over HTTPS for incognito and regular windows."""
    https_url = 'https://www.google.com'
    self._CookieCheckRegularWindow(https_url)
    self._CookieCheckIncognitoWindow(https_url)

  def testCookiesFile(self):
    """Test cookies set from an HTML file for incognito and regular windows."""
    file_url = self.GetFileURLForPath(
        os.path.join(self.DataDir(), 'setcookie.html'))
    self._CookieCheckRegularWindow(file_url)
    self._CookieCheckIncognitoWindow(file_url)

if __name__ == '__main__':
  pyauto_functional.Main()
<commit_msg>Fix cookies test<commit_after>#!/usr/bin/python
# Copyright (c) 2010 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import os

import pyauto_functional  # Must be imported before pyauto
import pyauto

class CookiesTest(pyauto.PyUITest):
  """Tests for Cookies."""

  def _CookieCheckIncognitoWindow(self, url):
    """Check the cookie for the given URL in an incognito window."""
    # Navigate to the URL in an incognito window and verify no cookie is set.
    self.RunCommand(pyauto.IDC_NEW_INCOGNITO_WINDOW)
    self.NavigateToURL(url, 1, 0)
    self.assertFalse(self.GetCookie(pyauto.GURL(url)))

  def testSetCookies(self):
    """Test setting cookies and getting the value."""
    cookie_url = pyauto.GURL(self.GetFileURLForPath(
        os.path.join(self.DataDir(), 'title1.html')))
    cookie_val = 'foo=bar'
    self.SetCookie(cookie_url, cookie_val)
    self.assertEqual(cookie_val, self.GetCookie(cookie_url))

  def testCookiesHttp(self):
    """Test cookies set over HTTP for incognito and regular windows."""
    http_url = 'http://www.google.com'
    self.assertFalse(self.GetCookie(pyauto.GURL(http_url)))
    # Incognito window
    self._CookieCheckIncognitoWindow(http_url)
    # Regular window
    self.NavigateToURL(http_url)
    cookie_data = self.GetCookie(pyauto.GURL(http_url))
    self.assertTrue(cookie_data)
    # Restart and verify that the cookie persists.
    self.assertEqual(cookie_data, self.GetCookie(pyauto.GURL(http_url)))

  def testCookiesHttps(self):
    """Test cookies set over HTTPS for incognito and regular windows."""
    https_url = 'https://www.google.com'
    self.assertFalse(self.GetCookie(pyauto.GURL(https_url)))
    # Incognito window
    self._CookieCheckIncognitoWindow(https_url)
    # Regular window
    self.NavigateToURL(https_url)
    cookie_data = self.GetCookie(pyauto.GURL(https_url))
    self.assertTrue(cookie_data)
    # Restart and verify that the cookie persists.
    self.assertEqual(cookie_data, self.GetCookie(pyauto.GURL(https_url)))

  def testCookiesFile(self):
    """Test cookies set from file:// url for incognito and regular windows."""
    file_url = self.GetFileURLForPath(
        os.path.join(self.DataDir(), 'setcookie.html'))
    self.assertFalse(self.GetCookie(pyauto.GURL(file_url)))
    # Incognito window
    self._CookieCheckIncognitoWindow(file_url)
    # Regular window
    self.NavigateToURL(file_url)
    self.assertEqual('name=Good', self.GetCookie(pyauto.GURL(file_url)))
    # Restart and verify that cookie persists
    self.RestartBrowser(clear_profile=False)
    self.assertEqual('name=Good', self.GetCookie(pyauto.GURL(file_url)))

if __name__ == '__main__':
  pyauto_functional.Main()
<|endoftext|>


<commit_before>#!/usr/bin/python

# Copyright (c) 2010 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import glob
import logging
import optparse
import os
import shutil
import sys
import tempfile
import zipfile

import pyauto_functional  # Must be imported before pyauto
import pyauto
import pyauto_utils

class ImportsTest(pyauto.PyUITest):
  """Import settings from other browsers.

  Import settings tables below show which items get imported on first run and
  via preferences for different browsers and operating systems.

            Bookmarks   History   SearchEngines   Passwords   Homepage
  Firefox:
  Win/FRUI      N          Y            N             N          Y
  Win/Prefs     Y          Y            Y             Y          N
  Mac&Lin/FRUI  Y          Y            Y             Y          Y
  Mac&Lin/Prefs Y          Y            Y             Y          N

  Safari:
  Mac/FRUI      Y          Y            Y             Y          N
  Mac/Prefs     Y          Y            Y             Y          N
  """
  def setUp(self):
    self._to_import = ['ALL']

    if pyauto.PyUITest.IsMac():
      self._firefox_profiles_path = os.path.join(
          os.environ['HOME'], 'Library','Application Support','Firefox')
      self._firefox_test_profile = os.path.abspath(os.path.join(
          pyauto.PyUITest.DataDir(), 'import', 'firefox', 'macwin.zip'))
      self._safari_profiles_path = os.path.join(
          os.environ['HOME'], 'Library', 'Safari')
      # Don't import passwords to avoid Keychain popups. See crbug.com/49378.
      self._to_import = ['HISTORY', 'FAVORITES', 'SEARCH_ENGINES', 'HOME_PAGE']
    elif pyauto.PyUITest.IsWin():
      self._firefox_profiles_path = os.path.join(
          os.getenv('APPDATA'), 'Mozilla', 'Firefox')
      self._firefox_test_profile = os.path.abspath(os.path.join(
          pyauto.PyUITest.DataDir(), 'import', 'firefox', 'macwin.zip'))
    else:  # Linux
      self._firefox_profiles_path = os.path.join(
          os.environ['HOME'], '.mozilla', 'firefox')
      self._firefox_test_profile = os.path.abspath(os.path.join(
          pyauto.PyUITest.DataDir(), 'import', 'firefox', 'linux.zip'))

    # Expected items for tests.
    self._history_items = ['Google', 'Google News', u'Google \ub3c4\uc11c']
    self._bookmark_bar_items = ['Google News']
    self._bookmark_folder_items = ['Google', u'Google \ub3c4\uc11c']
    self._password_items = ['etouchqa@gmail.com', 'macqa05']
    self._home_page = 'http://news.google.com/'

    self._safari_replacer = None
    self._firefox_replacer = None

    pyauto.PyUITest.setUp(self)

  def tearDown(self):
    pyauto.PyUITest.tearDown(self)
    # Delete any replacers to restore the original profiles.
    if self._safari_replacer:
      del self._safari_replacer
    if self._firefox_replacer:
      del self._firefox_replacer

  def _UnzipProfileToDir(self, profile_zip, dir):
    """Unzip |profile_zip| into directory |dir|.

    Creates |dir| if it doesn't exist.
    """
    zf = zipfile.ZipFile(profile_zip)
    # Make base.
    pushd = os.getcwd()
    try:
      if not os.path.isdir(dir):
        os.mkdir(dir)
      os.chdir(dir)
      # Extract files.
      for info in zf.infolist():
        name = info.filename
        if name.endswith('/'):  # It's a directory.
          if not os.path.isdir(name):
            os.makedirs(name)
        else:  # It's a file.
          dir = os.path.dirname(name)
          if dir and not os.path.isdir(dir):
            os.makedirs(dir)
          out = open(name, 'wb')
          out.write(zf.read(name))
          out.close()
        # Set permissions.
        os.chmod(name, 0777)
    finally:
      os.chdir(pushd)

  def _SwapFirefoxProfile(self):
    """Swaps the test Firefox profile with the original one."""
    self._firefox_replacer = pyauto_utils.ExistingPathReplacer(
        self._firefox_profiles_path)
    self._UnzipProfileToDir(self._firefox_test_profile,
                            self._firefox_profiles_path)

  def _SwapSafariProfile(self):
    """Swaps the test Safari profile with the original one."""
    self._safari_replacer = pyauto_utils.ExistingPathReplacer(
        self._safari_profiles_path)
    self._UnzipProfileToDir(
        os.path.join(self.DataDir(), 'import', 'safari', 'mac.zip'),
        self._safari_profiles_path)

  def _CheckForBookmarks(self, bookmark_titles, bookmark_bar):
    """Checks that the given bookmarks exist.

    Args:
      bookmark_titles: A set of bookmark title strings.
      bookmark_bar: True if the bookmarks are part of the bookmark bar.
                    False otherwise.
    """
    confirmed_titles = set()
    bookmarks = self.GetBookmarkModel()
    if bookmark_bar:
      node = bookmarks.BookmarkBar()
    else:
      node = bookmarks.Other()
    for title in bookmark_titles:
      self.assertTrue([x for x in bookmark_titles \
                       if bookmarks.FindByTitle(title, [node])])

  def _BookmarkDuplicatesExist(self, bookmark_titles):
    """Returns true if any of the bookmark titles are duplicated.

    Args:
      bookmark_titles: A list of bookmark title strings.
    """
    bookmarks = self.GetBookmarkModel()
    for title in bookmark_titles:
      if len(bookmarks.FindByTitle(title)) > 1:
        return True
    return False

  def _CheckForHistory(self, history_titles):
    """Verifies that the given list of history items are in the history.

    Args:
      history_titles: A list of history title strings.
    """
    history = self.GetHistoryInfo().History()

    # History import automation is broken - crbug.com/63001
    return

    for title in history_titles:
      self.assertTrue([x for x in history if x['title'] == title])

  def _CheckForPasswords(self, usernames):
    """Check that password items exist for the given usernames."""
    # Password import automation does not work on Mac. See crbug.com/52124.
    if self.IsMac():
      return
    passwords = self.GetSavedPasswords()
    for username in usernames:
      self.assertTrue([x for x in passwords if x['username_value'] == username])

  def _CheckDefaults(self, bookmarks, history, passwords, home_page,
                     search_engines):
    """Checks the defaults for each of the possible import items.

    All arguments are True if they should be checked, False otherwise."""
    if bookmarks:
      self._CheckForBookmarks(self._bookmark_bar_items, True)
      self._CheckForBookmarks(self._bookmark_folder_items, False)
    if history:
      self._CheckForHistory(self._history_items)
    if passwords:
      self._CheckForPasswords(self._password_items)
    if home_page:
      self.assertEqual(self._home_page, self.GetPrefsInfo().Prefs()['homepage'])
    # TODO(alyssad): Test for search engines after a hook is added.
    # See crbug.com/52009.

  def _CanRunFirefoxTests(self):
    """Determine whether we can run firefox imports.

    On windows, checks if firefox is installed. Always True on other platforms.
    """
    if self.IsWin():
      ff_installed = os.path.exists(os.path.join(
          os.getenv('ProgramFiles'), 'Mozilla Firefox', 'firefox.exe'))
      if not ff_installed:
        logging.warn('Firefox not installed.')
      return ff_installed
    # TODO(nirnimesh): Anything else to be done on other platforms?
    return True

  # Tests.
  def testFirefoxImportFromPrefs(self):
    """Verify importing Firefox data through preferences."""
    if not self._CanRunFirefoxTests():
      logging.warn('Not running firefox import tests.')
      return
    self._SwapFirefoxProfile()
    self.ImportSettings('Mozilla Firefox', False, self._to_import)
    self._CheckDefaults(bookmarks=True, history=True, passwords=True,
                        home_page=False, search_engines=True)

  def testFirefoxFirstRun(self):
    """Verify importing from Firefox on the first run.

    For Win, only history and homepage will only be imported.
    Mac and Linux can import history, homepage, and bookmarks.
    """
    if not self._CanRunFirefoxTests():
      logging.warn('Not running firefox import tests.')
      return
    self._SwapFirefoxProfile()
    self.ImportSettings('Mozilla Firefox', True, self._to_import)
    non_win = not self.IsWin()
    self._CheckDefaults(bookmarks=non_win, history=True, passwords=True,
                        home_page=non_win, search_engines=True)

  def testImportFirefoxDataTwice(self):
    """Verify importing Firefox data twice.

    Bookmarks should be duplicated, but history and passwords should not.
    """
    if not self._CanRunFirefoxTests():
      logging.warn('Not running firefox import tests.')
      return
    self._SwapFirefoxProfile()
    self.ImportSettings('Mozilla Firefox', False, self._to_import)
    num_history_orig = len(self.GetHistoryInfo().History())
    num_passwords_orig = len(self.GetSavedPasswords())

    # Re-import and check for duplicates.
    self.ImportSettings('Mozilla Firefox', False, self._to_import)
    self.assertTrue(self._BookmarkDuplicatesExist(
        self._bookmark_bar_items + self._bookmark_folder_items))
    self.assertEqual(num_history_orig, len(self.GetHistoryInfo().History()))
    self.assertEqual(num_passwords_orig, len(self.GetSavedPasswords()))

  def testImportFromFirefoxAndSafari(self):
    """Verify importing from Firefox and then Safari."""
    # This test is for Mac only.
    if not self.IsMac():
      return

    self._SwapSafariProfile()
    self._SwapFirefoxProfile()
    self.ImportSettings('Mozilla Firefox', False, self._to_import)
    self.ImportSettings('Safari', False, self._to_import)

    self._CheckDefaults(bookmarks=True, history=True, passwords=True,
                        home_page=False, search_engines=True)
    self.assertTrue(self._BookmarkDuplicatesExist(
        self._bookmark_bar_items + self._bookmark_folder_items))

  def testSafariImportFromPrefs(self):
    """Verify importing Safari data through preferences."""
    # This test is Mac only.
    if not self.IsMac():
      return
    self._SwapSafariProfile()
    self.ImportSettings('Safari', False, self._to_import)
    self._CheckDefaults(bookmarks=True, history=True, passwords=False,
                        home_page=False, search_engines=True)

  def testSafariFirstRun(self):
    """Verify importing Safari data on the first run."""
    # This test is Mac only.
    if not self.IsMac():
      return
    self._SwapSafariProfile()
    self.ImportSettings('Safari', False, self._to_import)
    self._CheckDefaults(bookmarks=True, history=True, passwords=False,
                        home_page=False, search_engines=False)

  def testImportSafariDataTwice(self):
    """Verify importing Safari data twice.

    Bookmarks should be duplicated, but history and passwords should not."""
    # This test is Mac only.
    if not self.IsMac():
      return
    self._SwapSafariProfile()
    self.ImportSettings('Safari', False, self._to_import)
    num_history_orig = len(self.GetHistoryInfo().History())
    num_passwords_orig = len(self.GetSavedPasswords())

    # Re-import and check for duplicates.
    self.ImportSettings('Safari', False, self._to_import)
    self.assertTrue(self._BookmarkDuplicatesExist(
        self._bookmark_bar_items + self._bookmark_folder_items))
    self.assertEqual(num_history_orig, len(self.GetHistoryInfo().History()))
    self.assertEqual(num_passwords_orig, len(self.GetSavedPasswords()))

if __name__ == '__main__':
  pyauto_functional.Main()
<commit_msg>Update expecations in pyauto import tests<commit_after>#!/usr/bin/python

# Copyright (c) 2011 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import glob
import logging
import optparse
import os
import shutil
import sys
import tempfile
import zipfile

import pyauto_functional  # Must be imported before pyauto
import pyauto
import pyauto_utils

class ImportsTest(pyauto.PyUITest):
  """Import settings from other browsers.

  Import settings tables below show which items get imported on first run and
  via preferences for different browsers and operating systems.

            Bookmarks   History   SearchEngines   Passwords   Homepage
  Firefox:
  Win/FRUI      N          Y            N             N          Y
  Win/Prefs     Y          Y            Y             Y          N
  Mac&Lin/FRUI  Y          Y            Y             Y          Y
  Mac&Lin/Prefs Y          Y            Y             Y          N

  Safari:
  Mac/FRUI      Y          Y            Y             Y          N
  Mac/Prefs     Y          Y            Y             Y          N
  """
  def setUp(self):
    self._to_import = ['ALL']

    if pyauto.PyUITest.IsMac():
      self._firefox_profiles_path = os.path.join(
          os.environ['HOME'], 'Library','Application Support','Firefox')
      self._firefox_test_profile = os.path.abspath(os.path.join(
          pyauto.PyUITest.DataDir(), 'import', 'firefox', 'macwin.zip'))
      self._safari_profiles_path = os.path.join(
          os.environ['HOME'], 'Library', 'Safari')
      # Don't import passwords to avoid Keychain popups. See crbug.com/49378.
      self._to_import = ['HISTORY', 'FAVORITES', 'SEARCH_ENGINES', 'HOME_PAGE']
    elif pyauto.PyUITest.IsWin():
      self._firefox_profiles_path = os.path.join(
          os.getenv('APPDATA'), 'Mozilla', 'Firefox')
      self._firefox_test_profile = os.path.abspath(os.path.join(
          pyauto.PyUITest.DataDir(), 'import', 'firefox', 'macwin.zip'))
    else:  # Linux
      self._firefox_profiles_path = os.path.join(
          os.environ['HOME'], '.mozilla', 'firefox')
      self._firefox_test_profile = os.path.abspath(os.path.join(
          pyauto.PyUITest.DataDir(), 'import', 'firefox', 'linux.zip'))

    # Expected items for tests.
    self._history_items = ['Google', 'Google News', u'Google \ub3c4\uc11c']
    self._bookmark_bar_items = ['Google News', 'Google', u'Google \ub3c4\uc11c']
    self._bookmark_folder_items = []
    self._password_items = ['etouchqa@gmail.com', 'macqa05']
    self._home_page = 'http://news.google.com/'

    self._safari_replacer = None
    self._firefox_replacer = None

    pyauto.PyUITest.setUp(self)

  def tearDown(self):
    pyauto.PyUITest.tearDown(self)
    # Delete any replacers to restore the original profiles.
    if self._safari_replacer:
      del self._safari_replacer
    if self._firefox_replacer:
      del self._firefox_replacer

  def _UnzipProfileToDir(self, profile_zip, dir):
    """Unzip |profile_zip| into directory |dir|.

    Creates |dir| if it doesn't exist.
    """
    zf = zipfile.ZipFile(profile_zip)
    # Make base.
    pushd = os.getcwd()
    try:
      if not os.path.isdir(dir):
        os.mkdir(dir)
      os.chdir(dir)
      # Extract files.
      for info in zf.infolist():
        name = info.filename
        if name.endswith('/'):  # It's a directory.
          if not os.path.isdir(name):
            os.makedirs(name)
        else:  # It's a file.
          dir = os.path.dirname(name)
          if dir and not os.path.isdir(dir):
            os.makedirs(dir)
          out = open(name, 'wb')
          out.write(zf.read(name))
          out.close()
        # Set permissions.
        os.chmod(name, 0777)
    finally:
      os.chdir(pushd)

  def _SwapFirefoxProfile(self):
    """Swaps the test Firefox profile with the original one."""
    self._firefox_replacer = pyauto_utils.ExistingPathReplacer(
        self._firefox_profiles_path)
    self._UnzipProfileToDir(self._firefox_test_profile,
                            self._firefox_profiles_path)

  def _SwapSafariProfile(self):
    """Swaps the test Safari profile with the original one."""
    self._safari_replacer = pyauto_utils.ExistingPathReplacer(
        self._safari_profiles_path)
    self._UnzipProfileToDir(
        os.path.join(self.DataDir(), 'import', 'safari', 'mac.zip'),
        self._safari_profiles_path)

  def _CheckForBookmarks(self, bookmark_titles, bookmark_bar):
    """Checks that the given bookmarks exist.

    Args:
      bookmark_titles: A set of bookmark title strings.
      bookmark_bar: True if the bookmarks are part of the bookmark bar.
                    False otherwise.
    """
    bookmarks = self.GetBookmarkModel()
    if bookmark_bar:
      node = bookmarks.BookmarkBar()
    else:
      node = bookmarks.Other()
    for title in bookmark_titles:
      self.assertTrue([x for x in bookmark_titles \
                       if bookmarks.FindByTitle(title, [node])])

  def _BookmarkDuplicatesExist(self, bookmark_titles):
    """Returns true if any of the bookmark titles are duplicated.

    Args:
      bookmark_titles: A list of bookmark title strings.
    """
    bookmarks = self.GetBookmarkModel()
    for title in bookmark_titles:
      if len(bookmarks.FindByTitle(title)) > 1:
        return True
    return False

  def _CheckForHistory(self, history_titles):
    """Verifies that the given list of history items are in the history.

    Args:
      history_titles: A list of history title strings.
    """
    history = self.GetHistoryInfo().History()

    # History import automation is broken - crbug.com/63001
    return

    for title in history_titles:
      self.assertTrue([x for x in history if x['title'] == title])

  def _CheckForPasswords(self, usernames):
    """Check that password items exist for the given usernames."""
    # Password import automation does not work on Mac. See crbug.com/52124.
    if self.IsMac():
      return
    passwords = self.GetSavedPasswords()
    for username in usernames:
      self.assertTrue([x for x in passwords if x['username_value'] == username])

  def _CheckDefaults(self, bookmarks, history, passwords, home_page,
                     search_engines):
    """Checks the defaults for each of the possible import items.

    All arguments are True if they should be checked, False otherwise."""
    if bookmarks:
      self._CheckForBookmarks(self._bookmark_bar_items, True)
      self._CheckForBookmarks(self._bookmark_folder_items, False)
    if history:
      self._CheckForHistory(self._history_items)
    if passwords:
      self._CheckForPasswords(self._password_items)
    if home_page:
      self.assertEqual(self._home_page, self.GetPrefsInfo().Prefs()['homepage'])
    # TODO(alyssad): Test for search engines after a hook is added.
    # See crbug.com/52009.

  def _CanRunFirefoxTests(self):
    """Determine whether we can run firefox imports.

    On windows, checks if firefox is installed. Always True on other platforms.
    """
    if self.IsWin():
      ff_installed = os.path.exists(os.path.join(
          os.getenv('ProgramFiles'), 'Mozilla Firefox', 'firefox.exe'))
      if not ff_installed:
        logging.warn('Firefox not installed.')
      return ff_installed
    # TODO(nirnimesh): Anything else to be done on other platforms?
    return True

  # Tests.
  def testFirefoxImportFromPrefs(self):
    """Verify importing Firefox data through preferences."""
    if not self._CanRunFirefoxTests():
      logging.warn('Not running firefox import tests.')
      return
    self._SwapFirefoxProfile()
    self.ImportSettings('Mozilla Firefox', False, self._to_import)
    self._CheckDefaults(bookmarks=True, history=True, passwords=True,
                        home_page=False, search_engines=True)

  def testFirefoxFirstRun(self):
    """Verify importing from Firefox on the first run.

    For Win, only history and homepage will only be imported.
    Mac and Linux can import history, homepage, and bookmarks.
    """
    if not self._CanRunFirefoxTests():
      logging.warn('Not running firefox import tests.')
      return
    self._SwapFirefoxProfile()
    self.ImportSettings('Mozilla Firefox', True, self._to_import)
    non_win = not self.IsWin()
    self._CheckDefaults(bookmarks=non_win, history=True, passwords=True,
                        home_page=non_win, search_engines=True)

  def testImportFirefoxDataTwice(self):
    """Verify importing Firefox data twice.

    Bookmarks should be duplicated, but history and passwords should not.
    """
    if not self._CanRunFirefoxTests():
      logging.warn('Not running firefox import tests.')
      return
    self._SwapFirefoxProfile()
    self.ImportSettings('Mozilla Firefox', False, self._to_import)
    num_history_orig = len(self.GetHistoryInfo().History())
    num_passwords_orig = len(self.GetSavedPasswords())

    # Re-import and check for duplicates.
    self.ImportSettings('Mozilla Firefox', False, self._to_import)
    self.assertTrue(self._BookmarkDuplicatesExist(
        self._bookmark_bar_items + self._bookmark_folder_items))
    self.assertEqual(num_history_orig, len(self.GetHistoryInfo().History()))
    self.assertEqual(num_passwords_orig, len(self.GetSavedPasswords()))

  def testImportFromFirefoxAndSafari(self):
    """Verify importing from Firefox and then Safari."""
    # This test is for Mac only.
    if not self.IsMac():
      return

    self._SwapSafariProfile()
    self._SwapFirefoxProfile()
    self.ImportSettings('Mozilla Firefox', False, self._to_import)
    self.ImportSettings('Safari', False, self._to_import)

    self._CheckDefaults(bookmarks=True, history=True, passwords=True,
                        home_page=False, search_engines=True)
    self.assertTrue(self._BookmarkDuplicatesExist(
        self._bookmark_bar_items + self._bookmark_folder_items))

  def testSafariImportFromPrefs(self):
    """Verify importing Safari data through preferences."""
    # This test is Mac only.
    if not self.IsMac():
      return
    self._SwapSafariProfile()
    self.ImportSettings('Safari', False, self._to_import)
    self._CheckDefaults(bookmarks=True, history=True, passwords=False,
                        home_page=False, search_engines=True)

  def testSafariFirstRun(self):
    """Verify importing Safari data on the first run."""
    # This test is Mac only.
    if not self.IsMac():
      return
    self._SwapSafariProfile()
    self.ImportSettings('Safari', False, self._to_import)
    self._CheckDefaults(bookmarks=True, history=True, passwords=False,
                        home_page=False, search_engines=False)

  def testImportSafariDataTwice(self):
    """Verify importing Safari data twice.

    Bookmarks should be duplicated, but history and passwords should not."""
    # This test is Mac only.
    if not self.IsMac():
      return
    self._SwapSafariProfile()
    self.ImportSettings('Safari', False, self._to_import)
    num_history_orig = len(self.GetHistoryInfo().History())
    num_passwords_orig = len(self.GetSavedPasswords())

    # Re-import and check for duplicates.
    self.ImportSettings('Safari', False, self._to_import)
    self.assertTrue(self._BookmarkDuplicatesExist(
        self._bookmark_bar_items + self._bookmark_folder_items))
    self.assertEqual(num_history_orig, len(self.GetHistoryInfo().History()))
    self.assertEqual(num_passwords_orig, len(self.GetSavedPasswords()))

if __name__ == '__main__':
  pyauto_functional.Main()
<|endoftext|>


<commit_before>#!/usr/bin/python
# Copyright (c) 2010 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import pyauto_functional  # Must be imported before pyauto
import pyauto

class PluginsTest(pyauto.PyUITest):
  """TestCase for Plugins."""

  def Debug(self):
    """Test method for experimentation.

    This method will not run automatically.
    """
    import pprint
    pp = pprint.PrettyPrinter(indent=2)
    while True:
      raw_input('Interact with the browser and hit <enter> to list plugins... ')
      pp.pprint(self.GetPluginsInfo().Plugins())

  def testHasFlash(self):
    """Verify that Flash plugin loads and is enabled."""
    flash = self.GetPluginsInfo().FirstPluginForName('Shockwave Flash')
    self.assertTrue(flash)
    self.assertTrue(flash['enabled'])

  def testEnableDisableFlash(self):
    """Verify that flash plugin can be enabled/disabled."""
    flash = self.GetPluginsInfo().FirstPluginForName('Shockwave Flash')
    self.assertTrue(flash['enabled'])
    self.DisablePlugin(flash['path'])
    flash = self.GetPluginsInfo().FirstPluginForName('Shockwave Flash')
    self.assertFalse(flash['enabled'])
    self.EnablePlugin(flash['path'])
    flash = self.GetPluginsInfo().FirstPluginForName('Shockwave Flash')
    self.assertTrue(flash['enabled'])

if __name__ == '__main__':
  pyauto_functional.Main()

<commit_msg>Adding plugins tests to plugins.py<commit_after>#!/usr/bin/python
# Copyright (c) 2010 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import logging
import os
import re

import pyauto_functional  # Must be imported before pyauto
import pyauto

class PluginsTest(pyauto.PyUITest):
  """TestCase for Plugins."""

  def Debug(self):
    """Test method for experimentation.

    This method will not run automatically.
    """
    import pprint
    pp = pprint.PrettyPrinter(indent=2)
    while True:
      raw_input('Interact with the browser and hit <enter> to list plugins...')
      pp.pprint(self.GetPluginsInfo().Plugins())

  def _ObtainPluginsList(self):
    """Obtain a list of plugins for each platform.

    Produces warnings for plugins which are not installed on the machine.

    Returns:
      a list of 2-tuple, corresponding to the html file used for test and the
      name of the plugin
    """
    plugins = [('flash-clicktoplay.html', 'Shockwave Flash'),
               ('java_new.html', 'Java'),]    # common to all platforms
    if self.IsWin() or self.IsMac():
      plugins = plugins + [
         ('silverlight_new.html', 'Silverlight'),
         ('quicktime.html', 'QuickTime'),
         ('wmp_new.html', 'Windows Media'),
         ('real.html', 'RealPlayer'),
      ]

    out = []
    # Emit warnings for plugins that are not installed on the machine and
    # therefore cannot be tested.
    plugins_info = self.GetPluginsInfo()
    for fname, name in plugins:
      for a_plugin in plugins_info.Plugins():
        is_installed = False
        if re.search(name, a_plugin['name']):
          is_installed = True
          break
      if not is_installed:
        logging.warn('%s plugin is not installed and cannot be tested' % name)
      else:
        out.append((fname, name))
    return out

  def _GetPluginPID(self, plugin_name):
    """Fetch the pid of the plugin process with name |plugin_name|."""
    child_processes = self.GetBrowserInfo()['child_processes']
    for x in child_processes:
       if x['type'] == 'Plug-in' and re.search(plugin_name, x['name']):
         return x['pid']
    return None

  def _TogglePlugin(self, plugin_name):
    """Toggle a plugin's status.

    If enabled, disable it.
    If disabled, enable it.
    """
    for plugin in self.GetPluginsInfo().Plugins():
      if re.search(plugin_name, plugin['name']):
        if plugin['enabled']:
          self.DisablePlugin(plugin['path'])
        else:
          self.EnablePlugin(plugin['path'])

  def _IsEnabled(self, plugin_name):
    """Checks if plugin is enabled."""
    for plugin in self.GetPluginsInfo().Plugins():
      if re.search(plugin_name, plugin['name']):
         return plugin['enabled']

  def testKillAndReloadAllPlugins(self):
    """Verify plugin processes and check if they can reload after killing."""
    for fname, plugin_name in self._ObtainPluginsList():
      url = self.GetFileURLForPath(
          os.path.join(self.DataDir(), 'plugin', fname))
      self.NavigateToURL(url)
      pid = self._GetPluginPID(plugin_name)
      self.assertTrue(pid, 'No plugin process for %s' % plugin_name)
      self.Kill(pid)
      self.assertTrue(self.WaitUntil(
          lambda: self._GetPluginPID(plugin_name) is None),
          msg='Expected %s plugin to die after killing' % plugin_name)
      self.GetBrowserWindow(0).GetTab(0).Reload()
      pid_reload = self._GetPluginPID(plugin_name)
      self.assertTrue(pid_reload,
                      'No plugin process for %s after reloading' % plugin_name)
      # Verify that it's in fact a new process.
      self.assertNotEqual(pid, pid_reload,
                          'Did not get new pid for %s after reloading' %
                          plugin_name)

  def testDisableEnableAllPlugins(self):
    """Verify if all the plugins can be disabled and enabled.

    This is equivalent to testing the enable/disable functionality in
    chrome://plugins
    """
    for fname, plugin_name in self._ObtainPluginsList():
      # Verify initial state
      self.assertTrue(self._IsEnabled(plugin_name),
                      '%s not enabled initially.' % plugin_name)
      # Disable
      self._TogglePlugin(plugin_name)
      self.assertFalse(self._IsEnabled(plugin_name))
      # Attempt to load a page that triggers the plugin and verify that it
      # indeed could not be loaded.
      url = self.GetFileURLForPath(
          os.path.join(self.DataDir(), 'plugin', fname))
      self.NavigateToURL(url)
      self.assertFalse([x for x in self.GetBrowserInfo()['child_processes']
                        if x['type'] == 'Plug-in' and
                        re.search(plugin_name, x['name'])])
      if 'Shockwave Flash' == plugin_name:
        continue  # cannot reload file:// flash URL - crbug.com/47249
      # Enable
      self._TogglePlugin(plugin_name)
      self.GetBrowserWindow(0).GetTab(0).Reload()
      self.assertTrue([x for x in self.GetBrowserInfo()['child_processes']
                       if x['type'] == 'Plug-in' and
                       re.search(plugin_name, x['name'])])
      self.assertTrue(self._IsEnabled(plugin_name), plugin_name)

if __name__ == '__main__':
  pyauto_functional.Main()
<|endoftext|>


<commit_before>from rest_framework.permissions import BasePermission
from .services import get_scope_from_request

import logging
logger = logging.getLogger(__name__)

READ_ONLY_METHODS = ('GET', 'HEAD', 'OPTIONS')
SCOPE_READ = "read"
SCOPE_WRITE = "write"

class BaseScopePermission(BasePermission):
    def __init__(self, *args, **kwargs):
        super(BaseScopePermission, self).__init__(*args, **kwargs)
        self.scope = None
        self.loaded_scope = False

    def has_permission(self, request, view):
        '''
        Implements has_permission().
        '''
        scope = self.get_scope_from_request(request)
        logger.debug("has_permission scope:%s" % scope)
        if scope is None:
            return True

        has_perm = self.has_scope_method_perm(request, view)
        logger.debug("has_permission:%s" % has_perm)
        return has_perm

    def has_object_permission(self, request, view, obj):
        '''
        Implements has_object_permission().
        '''
        scope = self.get_scope_from_request(request)
        logger.debug("has_object_permission scope:%s" % scope)
        if scope is None:
            return True

        has_method_perm = self.has_scope_method_perm(request, view)
        has_target_perm = self.has_scope_target_perm(request, view)
        has_object_perm = self.has_scope_object_perm(request, view, obj)

        has_perm = (has_method_perm and has_target_perm and has_object_perm)
        logger.debug("has_object_permission:%s (%s & %s & %s)" % (has_perm, has_method_perm, has_target_perm, has_object_perm))

        return has_perm

    def has_scope_method_perm(self, request, view):
        '''
        Utility method to check the request method type against the
        scope's permission (read or write).
        '''
        scope = self.get_scope_from_request(request)
        scope_perm = scope['permission']
        has_perm = False
        if scope_perm == SCOPE_WRITE:
            has_perm = True
        elif scope_perm == SCOPE_READ:
            has_perm = request.method in READ_ONLY_METHODS
        else:
            has_perm = False
        return has_perm

    def has_scope_target_perm(self, request, view):
        scope = self.get_scope_from_request(request)
        return scope['target'] == 'course'

    def has_scope_object_perm(self, request, view, obj):
        scope = self.get_scope_from_request(request)
        return scope['object'] == '*'

    def get_scope_from_request(self, request):
        if not self.loaded_scope:
            self.scope = get_scope_from_request(request)
            self.loaded_scope = True
        return self.scope

class CourseEndpointPermission(BaseScopePermission):
    """
    The request is authorized by the token scope.
    """
    def has_permission(self, request, view):
        scope = self.get_scope_from_request(request)
        has_perm = super(CourseEndpointPermission, self).has_permission(request, view)

        # Special case for creating a new course
        action = view.action_map.get(request.method.lower())
        if action == "create":
            has_perm = has_perm and (scope['object'] == '*')

        return has_perm

    def has_scope_object_perm(self, request, view, obj):
        scope = self.get_scope_from_request(request)
        has_perm = super(CourseEndpointPermission, self).has_scope_object_perm(request, view, obj)
        return has_perm or (str(scope['object']) == str(obj.pk))

class CollectionEndpointPermission(BaseScopePermission):
    """
    The request is authorized by the token scope.
    """
    def has_scope_object_perm(self, request, view, obj):
        scope = self.get_scope_from_request(request)
        has_perm = super(CollectionEndpointPermission, self).has_scope_object_perm(request, view, obj)
        return has_perm or (str(scope['object']) == str(obj.course.pk))

class ResourceEndpointPermission(CollectionEndpointPermission):
    """
    The request is authorized by the token scope.
    """
    pass

class CollectionResourceEndpointPermission(BaseScopePermission):
    """
    The request is authorized by the token scope.
    """
    def has_scope_object_perm(self, request, view, obj):
        scope = self.get_scope_from_request(request)
        has_perm = super(CollectionResourceEndpointPermission, self).has_scope_object_perm(request, view, obj)
        return has_perm or (str(scope['object']) == str(obj.collection.course.pk))
<commit_msg>Fixed bug.<commit_after>from rest_framework.permissions import BasePermission
from .services import get_scope_from_request

import logging
logger = logging.getLogger(__name__)

READ_ONLY_METHODS = ('GET', 'HEAD', 'OPTIONS')
SCOPE_READ = "read"
SCOPE_WRITE = "write"

class BaseScopePermission(BasePermission):
    def __init__(self, *args, **kwargs):
        super(BaseScopePermission, self).__init__(*args, **kwargs)
        self.scope = None
        self.loaded_scope = False

    def has_permission(self, request, view):
        '''
        Implements has_permission().
        '''
        scope = self.get_scope_from_request(request)
        logger.debug("has_permission scope:%s" % scope)
        if scope is None:
            return True

        has_perm = self.has_scope_method_perm(request, view)
        logger.debug("has_permission:%s" % has_perm)
        return has_perm

    def has_object_permission(self, request, view, obj):
        '''
        Implements has_object_permission().
        '''
        scope = self.get_scope_from_request(request)
        logger.debug("has_object_permission scope:%s" % scope)
        if scope is None:
            return True

        has_method_perm = self.has_scope_method_perm(request, view)
        has_target_perm = self.has_scope_target_perm(request, view)
        has_object_perm = self.has_scope_object_perm(request, view, obj)

        has_perm = (has_method_perm and has_target_perm and has_object_perm)
        logger.debug("has_object_permission:%s (%s & %s & %s)" % (has_perm, has_method_perm, has_target_perm, has_object_perm))

        return has_perm

    def has_scope_method_perm(self, request, view):
        '''
        Utility method to check the request method type against the
        scope's permission (read or write).
        '''
        scope = self.get_scope_from_request(request)
        scope_perm = scope['permission']
        has_perm = False
        if scope_perm == SCOPE_WRITE:
            has_perm = True
        elif scope_perm == SCOPE_READ:
            has_perm = request.method in READ_ONLY_METHODS
        else:
            has_perm = False
        return has_perm

    def has_scope_target_perm(self, request, view):
        scope = self.get_scope_from_request(request)
        return scope['target'] == 'course'

    def has_scope_object_perm(self, request, view, obj):
        scope = self.get_scope_from_request(request)
        return scope['object'] == '*'

    def get_scope_from_request(self, request):
        if not self.loaded_scope:
            self.scope = get_scope_from_request(request)
            self.loaded_scope = True
        return self.scope

class CourseEndpointPermission(BaseScopePermission):
    """
    The request is authorized by the token scope.
    """
    def has_permission(self, request, view):
        scope = self.get_scope_from_request(request)
        if scope is None:
            return True

        has_perm = super(CourseEndpointPermission, self).has_permission(request, view)

        # Special case for creating a new course
        action = view.action_map.get(request.method.lower())
        if action == "create":
            has_perm = has_perm and (scope['object'] == '*')

        return has_perm

    def has_scope_object_perm(self, request, view, obj):
        scope = self.get_scope_from_request(request)
        has_perm = super(CourseEndpointPermission, self).has_scope_object_perm(request, view, obj)
        return has_perm or (str(scope['object']) == str(obj.pk))

class CollectionEndpointPermission(BaseScopePermission):
    """
    The request is authorized by the token scope.
    """
    def has_scope_object_perm(self, request, view, obj):
        scope = self.get_scope_from_request(request)
        has_perm = super(CollectionEndpointPermission, self).has_scope_object_perm(request, view, obj)
        return has_perm or (str(scope['object']) == str(obj.course.pk))

class ResourceEndpointPermission(CollectionEndpointPermission):
    """
    The request is authorized by the token scope.
    """
    pass

class CollectionResourceEndpointPermission(BaseScopePermission):
    """
    The request is authorized by the token scope.
    """
    def has_scope_object_perm(self, request, view, obj):
        scope = self.get_scope_from_request(request)
        has_perm = super(CollectionResourceEndpointPermission, self).has_scope_object_perm(request, view, obj)
        return has_perm or (str(scope['object']) == str(obj.collection.course.pk))
<|endoftext|>


<commit_before># coding: utf-8
#
# pylint: disable=no-self-use, missing-docstring, too-many-public-methods, invalid-name
"""
Unit tests for OAI-PMH harvester.
"""
from unittest import TestCase

import bs4
from lxml import etree

import ckan
from ckanext.oaipmh.harvester import OAIPMHHarvester
import ckanext.harvest.model as harvest_model
import ckanext.kata.model as kata_model
from ckanext.oaipmh.importformats import create_metadata_registry
import ckanext.oaipmh.oai_dc_reader as dcr
from ckanext.oaipmh.oai_dc_reader import dc_metadata_reader
import lxml.etree
import os
from ckan import model
from ckan.logic import get_action
import json

FIXTURE_HELDA = "ckanext-oaipmh/ckanext/oaipmh/test_fixtures/helda_oai_dc.xml"
FIXTURE_IDA = "ckanext-oaipmh/ckanext/oaipmh/test_fixtures/oai-pmh.xml"

class _FakeHarvestSource():
    def __init__(self, config):
        self.config = json.dumps(config)

class _FakeHarvestObject():
    def __init__(self, content, identification, config):
        self.content = content
        self.id = identification
        self.guid = self.id
        self.source = _FakeHarvestSource(config)

    def add(self):
        pass

class TestOAIPMHHarvester(TestCase):

    @classmethod
    def setup_class(cls):
        '''
        Setup database and variables
        '''
        harvest_model.setup()
        kata_model.setup()
        cls.harvester = OAIPMHHarvester()

    @classmethod
    def teardown_class(cls):
        ckan.model.repo.rebuild_db()

    def test_harvester_info(self):
        info = self.harvester.info()
        assert isinstance(info, dict)

    def test_gather_stage(self):
        # gather_stage should throw some exception with parameter None
        self.assertRaises(Exception, self.harvester.gather_stage, (None))

    def test_fetch_stage(self):
        # should throw some exception with parameter None
        self.assertRaises(Exception, self.harvester.fetch_stage, (None))

    def test_import_stage(self):
        assert not self.harvester.import_stage(None)

    def _get_fixture(self, filename):
        return os.path.join(os.path.dirname(__file__), "..", "test_fixtures", filename)

    def manual_test_import_stage(self):
        model.User(name='harvest', sysadmin=True).save()
        get_action('organization_create')({'user': 'harvest'}, {'name': 'test'})

        tree = lxml.etree.parse(self._get_fixture('ida.xml'))
        record = tree.xpath('/oai:OAI-PMH/*/oai:record', namespaces={'oai': 'http://www.openarchives.org/OAI/2.0/'})[0]

        metadata = dc_metadata_reader('ida')(record)
        metadata['unified']['owner_org'] = "test"
        harvest_object = _FakeHarvestObject(json.dumps(metadata.getMap()), "test_id", {'type': 'ida'})
        self.harvester.import_stage(harvest_object)
        package = model.Session.query(model.Package).all()[0]
        self.assertTrue('direct_download' not in package.notes)

    def test_validate_config_valid(self):
        config = '{"from": "2014-03-03", "limit": 5}'

        config = self.harvester.validate_config(config)

        assert isinstance(config, basestring)
        assert 'limit' in config

    def test_validate_config_invalid(self):
        config = '{"from": 100, "limit": 5}'

        # 'from' is not a string so should throw an error
        self.assertRaises(TypeError, self.harvester.validate_config, (config))

# class TestImportCore(TestCase):
#
#     def test_harvester_info(self):
#         parse_xml = generic_xml_metadata_reader
#         assert isinstance(info, dict)

class TestOAIDCReaderHelda(TestCase):
    '''
    Tests for reading OAI_DC metadata generated from Helda
    '''

    @classmethod
    def setup_class(cls):
        '''
        Setup variables
        '''
        cls.xml_file = open(FIXTURE_HELDA, 'r')
        cls.xml = cls.xml_file.read()

        cls.bs = bs4.BeautifulSoup(cls.xml, 'xml')
        cls.dc = cls.bs.metadata.dc

    @classmethod
    def teardown_class(cls):
        pass

    def test_dc_metadata_reader(self):
        '''
        Test reading a whole file
        '''

        metadata = dcr.dc_metadata_reader('default')(etree.fromstring(self.xml))

        assert metadata

        assert 'unified' in metadata.getMap()
        assert 'availability' in metadata.getMap()['unified']

    def test_filter_tag_name_namespace(self):
        output = dcr._filter_tag_name_namespace('creator', dcr.NS['dc'])
        creators = [creator for creator in self.dc(output, recursive=False)]

        assert len(creators) == 3

    def test_get_data_pids(self):
        expected_pids = set([u'http://link.aip.org/link/?jcp/123/064507', u'http://hdl.handle.net/10138/1074'])
        output = dcr._get_data_pids(self.dc)

        assert set(output) == expected_pids

    def test_get_download(self):
        output = dcr._get_download(self.dc)

        # We should get atleast some download link from the pids:
        assert len(list(output)) > 0

    def test_get_org_auth(self):
        output = dcr._get_org_auth(self.dc)

        org_auth = []
        auths = []
        for row in output:
            org_auth.append(row)
            auths.append(row.get('value'))

        assert org_auth
        assert auths

        assert u"Khriachtchev, Leonid" in auths
        assert u"Räsänen, Markku" in auths

    def test_get_rights(self):
        output = dcr._get_rights(self.dc)

        values = list(output)
        assert len(values) == 4

        license_url = values[2]

        assert license_url
        assert license_url.startswith('Copyright')

    def test_dc_metadata_reader_fields(self):
        '''
        Test reading a whole file and check that fields are what they are supposed to be
        '''
        EXPECTED_FIELDS = {'access_application_URL': '',
                           'access_request_URL': '',
                           'algorithm': '',
                           'availability': 'through_provider',
                           'checksum': '',
                           'contact': [{'URL': '', 'phone': '', 'email': '', 'name': ''}],
                           'direct_download_URL': u'http://link.aip.org/link/?jcp/123/064507',
                           'discipline': '',
                           'geographic_coverage': '',
                           'langtitle': [{'lang': '',
                                          'value': u'Neutralization of solvated protons and formation of noble-gas hydride molecules: matrix-isolation indications of tunneling mechanisms?'}],
                           'language': u'en',
                           'license_URL': u'Copyright 2005 American Institute of Physics. This article may be downloaded for personal use only. Any other use requires prior permission of the author and the American Institute of Physics.',
                           'license_id': 'notspecified',
                           'mimetype': '',
                           'name': u'http%3A%2F%2Flink.aip.org%2Flink%2F%3Fjcp%2F123%2F064507',
                           'notes': '',
                           'pids': [{'id': u'http://hdl.handle.net/10138/1074',
                                     'provider': u'http://helda.helsinki.fi/oai/request',
                                     'type': 'data'},
                                    ],
                           'tag_string': '',
                           'temporal_coverage_begin': '',
                           'temporal_coverage_end': '',
                           'through_provider_URL': u'http://link.aip.org/link/?jcp/123/064507',
                           'type': 'dataset',
                           'version': u'2005-08-08'}

        metadata = dcr.dc_metadata_reader('default')(etree.fromstring(self.xml))
        assert metadata

        data_dict = metadata['unified']

        for (key, value) in EXPECTED_FIELDS.items():
            assert key in data_dict, "Key not found: %r" % key

            output_value = data_dict.get(key)

            # Note. Possibility for random fail, because data order is not promised by python
            assert unicode(output_value) == unicode(value), "Values for key %r not matching: %r versus %r" % (
                key, value, output_value)

        fail_agent = 1
        fail_author = 3
        for agent in data_dict.get('agent', []):
            if agent['role'] == 'funder':
                for key, value in ('URL', ''), ('id', ''), ('fundingid', ''), ('name', ''):
                    self.assertTrue(key in agent, "Expected to find key %s" % key)
                    self.assertEquals(agent[key], value)
                fail_agent -= 1
            elif agent['role'] == 'author':
                self.assertTrue(agent['name'] in (u'Khriachtchev, Leonid', u'Lignell, Antti', u'R\xe4s\xe4nen, Markku'))
                fail_author -= 1

        self.assertEqual(fail_agent, 0, "Invalid agent data")
        self.assertEqual(fail_author, 0, "Invalid author data")

    # TODO: Implement this in harvester first
    # def test_get_provider(self):
    #     output = dcr._get_provider(self.dc)
    #
    #     assert output == u'http://helda.helsinki.fi/oai/request', output

class TestOAIDCReaderIda(TestCase):

    @classmethod
    def setup_class(cls):
        '''
        Setup variables
        '''
        cls.xml_file = open(FIXTURE_IDA, 'r')
        cls.xml = cls.xml_file.read()

        cls.bs = bs4.BeautifulSoup(cls.xml, 'xml')
        cls.dc = cls.bs.metadata.dc

    @classmethod
    def teardown_class(cls):
        pass

    def test_dc_metadata_reader(self):
        '''
        Test reading a whole file
        '''

        metadata = dcr.dc_metadata_reader('default')(etree.fromstring(self.xml))

        assert metadata

        assert 'unified' in metadata.getMap()
        assert 'availability' in metadata.getMap()['unified']

    def test_get_version_pid(self):
        pid = dcr._get_version_pid(self.dc)

        assert pid
        assert 'ida' in next(pid)

    def test_get_checksum(self):
        hash = dcr._get_checksum(self.dc)

        assert hash == u'7932df5999a30bb70871359f700dbe23'

    def test_get_download(self):
        output = dcr._get_download(self.dc)

        # We should get atleast some download link:
        assert len(list(output)) > 0

    def test_get_provider(self):
        output = dcr._get_provider(self.dc)

        assert output == 'ida', output

class TestImportFormats(TestCase):
    def test_create_metadata_registry(self):
        reg = create_metadata_registry()

        assert reg
        assert reg.hasReader('oai_dc')
<commit_msg>Renamed manual test<commit_after># coding: utf-8
#
# pylint: disable=no-self-use, missing-docstring, too-many-public-methods, invalid-name
"""
Unit tests for OAI-PMH harvester.
"""
from unittest import TestCase

import bs4
from lxml import etree

import ckan
from ckanext.oaipmh.harvester import OAIPMHHarvester
import ckanext.harvest.model as harvest_model
import ckanext.kata.model as kata_model
from ckanext.oaipmh.importformats import create_metadata_registry
import ckanext.oaipmh.oai_dc_reader as dcr
from ckanext.oaipmh.oai_dc_reader import dc_metadata_reader
import lxml.etree
import os
from ckan import model
from ckan.logic import get_action
import json

FIXTURE_HELDA = "ckanext-oaipmh/ckanext/oaipmh/test_fixtures/helda_oai_dc.xml"
FIXTURE_IDA = "ckanext-oaipmh/ckanext/oaipmh/test_fixtures/oai-pmh.xml"

class _FakeHarvestSource():
    def __init__(self, config):
        self.config = json.dumps(config)

class _FakeHarvestObject():
    def __init__(self, content, identification, config):
        self.content = content
        self.id = identification
        self.guid = self.id
        self.source = _FakeHarvestSource(config)

    def add(self):
        pass

class TestOAIPMHHarvester(TestCase):

    @classmethod
    def setup_class(cls):
        '''
        Setup database and variables
        '''
        harvest_model.setup()
        kata_model.setup()
        cls.harvester = OAIPMHHarvester()

    @classmethod
    def teardown_class(cls):
        ckan.model.repo.rebuild_db()

    def test_harvester_info(self):
        info = self.harvester.info()
        assert isinstance(info, dict)

    def test_gather_stage(self):
        # gather_stage should throw some exception with parameter None
        self.assertRaises(Exception, self.harvester.gather_stage, (None))

    def test_fetch_stage(self):
        # should throw some exception with parameter None
        self.assertRaises(Exception, self.harvester.fetch_stage, (None))

    def test_import_stage(self):
        assert not self.harvester.import_stage(None)

    def _get_fixture(self, filename):
        return os.path.join(os.path.dirname(__file__), "..", "test_fixtures", filename)

    def manual_import_stage(self):
        model.User(name='harvest', sysadmin=True).save()
        get_action('organization_create')({'user': 'harvest'}, {'name': 'test'})

        tree = lxml.etree.parse(self._get_fixture('ida.xml'))
        record = tree.xpath('/oai:OAI-PMH/*/oai:record', namespaces={'oai': 'http://www.openarchives.org/OAI/2.0/'})[0]

        metadata = dc_metadata_reader('ida')(record)
        metadata['unified']['owner_org'] = "test"
        harvest_object = _FakeHarvestObject(json.dumps(metadata.getMap()), "test_id", {'type': 'ida'})
        self.harvester.import_stage(harvest_object)
        package = model.Session.query(model.Package).all()[0]
        self.assertTrue('direct_download' not in package.notes)

    def test_validate_config_valid(self):
        config = '{"from": "2014-03-03", "limit": 5}'

        config = self.harvester.validate_config(config)

        assert isinstance(config, basestring)
        assert 'limit' in config

    def test_validate_config_invalid(self):
        config = '{"from": 100, "limit": 5}'

        # 'from' is not a string so should throw an error
        self.assertRaises(TypeError, self.harvester.validate_config, (config))

# class TestImportCore(TestCase):
#
#     def test_harvester_info(self):
#         parse_xml = generic_xml_metadata_reader
#         assert isinstance(info, dict)

class TestOAIDCReaderHelda(TestCase):
    '''
    Tests for reading OAI_DC metadata generated from Helda
    '''

    @classmethod
    def setup_class(cls):
        '''
        Setup variables
        '''
        cls.xml_file = open(FIXTURE_HELDA, 'r')
        cls.xml = cls.xml_file.read()

        cls.bs = bs4.BeautifulSoup(cls.xml, 'xml')
        cls.dc = cls.bs.metadata.dc

    @classmethod
    def teardown_class(cls):
        pass

    def test_dc_metadata_reader(self):
        '''
        Test reading a whole file
        '''

        metadata = dcr.dc_metadata_reader('default')(etree.fromstring(self.xml))

        assert metadata

        assert 'unified' in metadata.getMap()
        assert 'availability' in metadata.getMap()['unified']

    def test_filter_tag_name_namespace(self):
        output = dcr._filter_tag_name_namespace('creator', dcr.NS['dc'])
        creators = [creator for creator in self.dc(output, recursive=False)]

        assert len(creators) == 3

    def test_get_data_pids(self):
        expected_pids = set([u'http://link.aip.org/link/?jcp/123/064507', u'http://hdl.handle.net/10138/1074'])
        output = dcr._get_data_pids(self.dc)

        assert set(output) == expected_pids

    def test_get_download(self):
        output = dcr._get_download(self.dc)

        # We should get atleast some download link from the pids:
        assert len(list(output)) > 0

    def test_get_org_auth(self):
        output = dcr._get_org_auth(self.dc)

        org_auth = []
        auths = []
        for row in output:
            org_auth.append(row)
            auths.append(row.get('value'))

        assert org_auth
        assert auths

        assert u"Khriachtchev, Leonid" in auths
        assert u"Räsänen, Markku" in auths

    def test_get_rights(self):
        output = dcr._get_rights(self.dc)

        values = list(output)
        assert len(values) == 4

        license_url = values[2]

        assert license_url
        assert license_url.startswith('Copyright')

    def test_dc_metadata_reader_fields(self):
        '''
        Test reading a whole file and check that fields are what they are supposed to be
        '''
        EXPECTED_FIELDS = {'access_application_URL': '',
                           'access_request_URL': '',
                           'algorithm': '',
                           'availability': 'through_provider',
                           'checksum': '',
                           'contact': [{'URL': '', 'phone': '', 'email': '', 'name': ''}],
                           'direct_download_URL': u'http://link.aip.org/link/?jcp/123/064507',
                           'discipline': '',
                           'geographic_coverage': '',
                           'langtitle': [{'lang': '',
                                          'value': u'Neutralization of solvated protons and formation of noble-gas hydride molecules: matrix-isolation indications of tunneling mechanisms?'}],
                           'language': u'en',
                           'license_URL': u'Copyright 2005 American Institute of Physics. This article may be downloaded for personal use only. Any other use requires prior permission of the author and the American Institute of Physics.',
                           'license_id': 'notspecified',
                           'mimetype': '',
                           'name': u'http%3A%2F%2Flink.aip.org%2Flink%2F%3Fjcp%2F123%2F064507',
                           'notes': '',
                           'pids': [{'id': u'http://hdl.handle.net/10138/1074',
                                     'provider': u'http://helda.helsinki.fi/oai/request',
                                     'type': 'data'},
                                    ],
                           'tag_string': '',
                           'temporal_coverage_begin': '',
                           'temporal_coverage_end': '',
                           'through_provider_URL': u'http://link.aip.org/link/?jcp/123/064507',
                           'type': 'dataset',
                           'version': u'2005-08-08'}

        metadata = dcr.dc_metadata_reader('default')(etree.fromstring(self.xml))
        assert metadata

        data_dict = metadata['unified']

        for (key, value) in EXPECTED_FIELDS.items():
            assert key in data_dict, "Key not found: %r" % key

            output_value = data_dict.get(key)

            # Note. Possibility for random fail, because data order is not promised by python
            assert unicode(output_value) == unicode(value), "Values for key %r not matching: %r versus %r" % (
                key, value, output_value)

        fail_agent = 1
        fail_author = 3
        for agent in data_dict.get('agent', []):
            if agent['role'] == 'funder':
                for key, value in ('URL', ''), ('id', ''), ('fundingid', ''), ('name', ''):
                    self.assertTrue(key in agent, "Expected to find key %s" % key)
                    self.assertEquals(agent[key], value)
                fail_agent -= 1
            elif agent['role'] == 'author':
                self.assertTrue(agent['name'] in (u'Khriachtchev, Leonid', u'Lignell, Antti', u'R\xe4s\xe4nen, Markku'))
                fail_author -= 1

        self.assertEqual(fail_agent, 0, "Invalid agent data")
        self.assertEqual(fail_author, 0, "Invalid author data")

    # TODO: Implement this in harvester first
    # def test_get_provider(self):
    #     output = dcr._get_provider(self.dc)
    #
    #     assert output == u'http://helda.helsinki.fi/oai/request', output

class TestOAIDCReaderIda(TestCase):

    @classmethod
    def setup_class(cls):
        '''
        Setup variables
        '''
        cls.xml_file = open(FIXTURE_IDA, 'r')
        cls.xml = cls.xml_file.read()

        cls.bs = bs4.BeautifulSoup(cls.xml, 'xml')
        cls.dc = cls.bs.metadata.dc

    @classmethod
    def teardown_class(cls):
        pass

    def test_dc_metadata_reader(self):
        '''
        Test reading a whole file
        '''

        metadata = dcr.dc_metadata_reader('default')(etree.fromstring(self.xml))

        assert metadata

        assert 'unified' in metadata.getMap()
        assert 'availability' in metadata.getMap()['unified']

    def test_get_version_pid(self):
        pid = dcr._get_version_pid(self.dc)

        assert pid
        assert 'ida' in next(pid)

    def test_get_checksum(self):
        hash = dcr._get_checksum(self.dc)

        assert hash == u'7932df5999a30bb70871359f700dbe23'

    def test_get_download(self):
        output = dcr._get_download(self.dc)

        # We should get atleast some download link:
        assert len(list(output)) > 0

    def test_get_provider(self):
        output = dcr._get_provider(self.dc)

        assert output == 'ida', output

class TestImportFormats(TestCase):
    def test_create_metadata_registry(self):
        reg = create_metadata_registry()

        assert reg
        assert reg.hasReader('oai_dc')
<|endoftext|>


<commit_before>import logging

from routes.mapper import SubMapper

from ckan import plugins
from ckan.plugins import toolkit

from ckanext.project.logic import schema as project_schema
from ckanext.project.model import setup as model_setup
from ckanext.project.logic import action

log = logging.getLogger(__name__)

DATASET_TYPE_NAME = 'project'

class projectPlugin(plugins.SingletonPlugin, toolkit.DefaultDatasetForm):
    plugins.implements(plugins.IConfigurable)
    plugins.implements(plugins.IConfigurer)
    plugins.implements(plugins.IDatasetForm)
    plugins.implements(plugins.IRoutes, inherit=True)
    plugins.implements(plugins.IAuthFunctions)
    plugins.implements(plugins.IActions)
    plugins.implements(plugins.IPackageController)

    # IConfigurer
    def update_config(self, config):
        toolkit.add_template_directory(config, '../templates')
        toolkit.add_public_directory(config, '../public')
        toolkit.add_resource('../fanstatic', 'project')

    # IConfigurable
    def configure(self, config):
        model_setup()

    # IDatasetForm
    def package_types(self):
        return [DATASET_TYPE_NAME]

    def is_fallback(self):
        return True

    def search_template(self):
        return 'project/search.html'

    def new_template(self):
        return 'project/new.html'

    def read_template(self):
        return 'project/read.html'

#    def edit_template(self):
#        return 'project/edit.html'

    def resource_form(self):
        return 'project/snippets/resource_form.html'

    def package_form(self):
        return 'project/new_package_form.html'

    def create_package_schema(self):
        return project_schema.project_create_schema()

    def update_package_schema(self):
        return project_schema.project_update_schema()

    def show_package_schema(self):
        return project_schema.project_show_schema()

    # IRoutes
    def before_map(self, map):

        # rerouting existing CKAN routes
        map.redirect('/projects', '/project')
        map.redirect('/projects/{url:.*}', '/project/{url}')

        map.redirect('/group/{url:.*}', '/organization/{url}',
                     _redirect_code='301 Moved Permanently')
        map.redirect('/group', '/organization',
                     _redirect_code='301 Moved Permanently')
        map.redirect('/groups/{url:.*}', '/organization/{url}',
                     _redirect_code='301 Moved Permanently')
        map.redirect('/groups', '/organization',
                     _redirect_code='301 Moved Permanently')

        map.connect('project_new_resource', '/project/new_resource/{id}',
                    controller='package', action='new_resource')
        map.redirect('/dataset/{url:.*}', '/project/{url}',
                     _redirect_code='301 Moved Permanently')
        map.redirect('/dataset', '/project',
                     _redirect_code='301 Moved Permanently')
        map.redirect('/datasets/{url:.*}', '/project/{url}',
                     _redirect_code='301 Moved Permanently')
        map.redirect('/datasets', '/project',
                     _redirect_code='301 Moved Permanently')
        map.redirect('/projects', '/project',
                      _redirect_code='301 Moved Permanently')

        # remap login function to projects page rather than activity log
        # done for demo 10/14
        map.redirect('/dashboard', '/project')

        with SubMapper(map, controller='ckanext.project.controller:projectController') as m:
            m.connect('project_new', '/project/new', action='new')
            m.connect('project_edit', '/project/edit', action='edit')
            m.connect('project_delete', '/project/delete/{id}', action='delete')
        #new_routes

        controller = 'ckanext.project.upload_controller:Upload_Controller'
        parcel_controller = 'ckanext.project.parcel_controller:Parcel_Controller'
        map_controller = 'ckanext.project.map_controller:Map_Controller'
        relationship = 'ckanext.project.relationship_controller:Cadasta_Relationship_Controller'

        #project
        map.connect('project_surveys', '/project/{id}/surveys', controller=controller, action='show_surveys')
        map.connect('project_parcels', '/project/{id}/parcels', controller=parcel_controller, action='read_parcels')
        map.connect('project_resources', '/project/{id}/resources', controller=controller, action='read_resources')
        map.connect('project_activity_stream', '/project/{id}/activity_stream', controller=controller, action='read_activity_stream')
        map.connect('project_survey_template', '/project/{id}/survey_template', controller=controller, action='read_survey_template')
        map.connect('project_form_data_upload', '/project/{id}/form_data_upload', controller=controller, action='form_data_upload')

        #big map
        map.connect('project_map', '/project/{id}/map', controller=map_controller, action='show_map')
        map.connect('edit_project_map', '/project/{id}/edit_map', controller=map_controller, action='edit_map')
        map.connect('show_parcel_map', '/project/{id}/parcel/{parcel_id}/map', controller=map_controller, action='show_parcel_map')
        map.connect('edit_parcel_map', '/project/{id}/edit_parcel/{parcel_id}/map', controller=map_controller, action='edit_parcel_map')
        map.connect('new_parcel_map', '/project/{id}/new/parcel/map', controller=map_controller, action='new_parcel_map')

        map.connect('show_relationship_map', '/project/{id}/parcel/{parcel_id}/relationship/{relationship_id}/map', controller=relationship, action='show_relationship_map')
        map.connect('edit_relationship_map', '/project/{id}/parcel/{parcel_id}/edit_relationship/{relationship_id}/map', controller=relationship, action='edit_relationship_map')
        map.connect('new_relationships_map', '/project/{id}/parcel/{parcel_id}/new/relationship/map', controller=relationship, action='new_relationship_map')

        #parcels
        map.connect('parcel_details', '/project/{id}/parcel/{parcel_id}', controller=parcel_controller, action='read_parcel_details')
        map.connect('edit_parcel_details', '/project/{id}/edit_parcel/{parcel_id}', controller=parcel_controller, action='edit_parcel_details')
        map.connect('new_parcel', '/project/{id}/new/parcel', controller=parcel_controller, action='new_parcel')

        #surveys
        map.connect('survey_details', '/project/{id}/survey/{survey_id}', controller=controller, action='read_survey_details')
        map.connect('edit_survey_details', '/project/{id}/edit_survey/{survey_id}', controller=controller, action='edit_survey_details')

        #relationship
        map.connect('relationship_details', '/project/{id}/parcel/{parcel_id}/relationship/{relationship_id}', controller=relationship, action='read_relationship_details')
        map.connect('edit_relationship_details', '/project/{id}/parcel/{parcel_id}/edit_relationship/{relationship_id}', controller=relationship, action='edit_relationship_details')
        map.connect('new_relationship', '/project/{id}/parcel/{parcel_id}/new/relationship', controller=relationship, action='new_relationship')
        map.connect('relationship_history', '/project/{id}/parcel/{parcel_id}/relationship_history', controller=relationship, action='get_relationship_history')

        #people/parties
        map.connect('party', '/project/{id}/party/{party_id}', controller=controller, action='read_party_details')
        map.connect('edit_party_details', '/project/{id}/edit_party/{party_id}', controller=controller, action='edit_party_details')
        map.connect('new_party', '/project/{id}/new/party', controller=controller, action='new_party')

        return map

    # IAuthFunctions
    def get_auth_functions(self):
        return {}

    # IActions
    def get_actions(self):
        return dict((name, function) for name, function
                    in action.__dict__.items()
                    if callable(function))

    # IPackageController
    def after_delete(self, context, package_dict):
        project_schema.project_archive(package_dict,context)

    def after_search(self, search_results, search_params):
        return search_results

    def after_show(self, context, package_dict):
        pass

    def after_update(self, context, package_dict):
        pass

    def after_create(self, context, package_dict):
        pass

    def before_search(self, search_params):
        return search_params

    def before_view(self, package_dict):
        return package_dict

    def before_index(self,package_dict):
        return package_dict

    def read(self, entity):
        pass

    def create(self, entity):
        pass

    def edit(self, entity):
        pass

    def authz_add_role(self, object_role):
        pass

    def authz_remove_role(self, object_role):
        pass

    def delete(self, entity):
        pass

<commit_msg>[#83]: redirect some routes back to /project and remove map specific one<commit_after>import logging

from routes.mapper import SubMapper

from ckan import plugins
from ckan.plugins import toolkit

from ckanext.project.logic import schema as project_schema
from ckanext.project.model import setup as model_setup
from ckanext.project.logic import action

log = logging.getLogger(__name__)

DATASET_TYPE_NAME = 'project'

class projectPlugin(plugins.SingletonPlugin, toolkit.DefaultDatasetForm):
    plugins.implements(plugins.IConfigurable)
    plugins.implements(plugins.IConfigurer)
    plugins.implements(plugins.IDatasetForm)
    plugins.implements(plugins.IRoutes, inherit=True)
    plugins.implements(plugins.IAuthFunctions)
    plugins.implements(plugins.IActions)
    plugins.implements(plugins.IPackageController)

    # IConfigurer
    def update_config(self, config):
        toolkit.add_template_directory(config, '../templates')
        toolkit.add_public_directory(config, '../public')
        toolkit.add_resource('../fanstatic', 'project')

    # IConfigurable
    def configure(self, config):
        model_setup()

    # IDatasetForm
    def package_types(self):
        return [DATASET_TYPE_NAME]

    def is_fallback(self):
        return True

    def search_template(self):
        return 'project/search.html'

    def new_template(self):
        return 'project/new.html'

    def read_template(self):
        return 'project/read.html'

#    def edit_template(self):
#        return 'project/edit.html'

    def resource_form(self):
        return 'project/snippets/resource_form.html'

    def package_form(self):
        return 'project/new_package_form.html'

    def create_package_schema(self):
        return project_schema.project_create_schema()

    def update_package_schema(self):
        return project_schema.project_update_schema()

    def show_package_schema(self):
        return project_schema.project_show_schema()

    # IRoutes
    def before_map(self, map):

        # rerouting existing CKAN routes
        map.redirect('/projects', '/project')
        map.redirect('/projects/{url:.*}', '/project/{url}')

        map.redirect('/group/{url:.*}', '/organization/{url}',
                     _redirect_code='301 Moved Permanently')
        map.redirect('/group', '/organization',
                     _redirect_code='301 Moved Permanently')
        map.redirect('/groups/{url:.*}', '/organization/{url}',
                     _redirect_code='301 Moved Permanently')
        map.redirect('/groups', '/organization',
                     _redirect_code='301 Moved Permanently')

        map.connect('project_new_resource', '/project/new_resource/{id}',
                    controller='package', action='new_resource')
        map.redirect('/dataset/{url:.*}', '/project/{url}',
                     _redirect_code='301 Moved Permanently')
        map.redirect('/dataset', '/project',
                     _redirect_code='301 Moved Permanently')
        map.redirect('/datasets/{url:.*}', '/project/{url}',
                     _redirect_code='301 Moved Permanently')
        map.redirect('/datasets', '/project',
                     _redirect_code='301 Moved Permanently')
        map.redirect('/projects', '/project',
                      _redirect_code='301 Moved Permanently')
        map.redirect('/tag', '/project',
                      _redirect_code='301 Moved Permanently')
        map.redirect('/stats', '/project',
                      _redirect_code='301 Moved Permanently')
        map.redirect('/revision', '/project',
                      _redirect_code='301 Moved Permanently')

        # remap login function to projects page rather than activity log
        # done for demo 10/14
        map.redirect('/dashboard', '/project')

        with SubMapper(map, controller='ckanext.project.controller:projectController') as m:
            m.connect('project_new', '/project/new', action='new')
            m.connect('project_edit', '/project/edit', action='edit')
            m.connect('project_delete', '/project/delete/{id}', action='delete')
        #new_routes

        controller = 'ckanext.project.upload_controller:Upload_Controller'
        parcel_controller = 'ckanext.project.parcel_controller:Parcel_Controller'
        map_controller = 'ckanext.project.map_controller:Map_Controller'
        relationship = 'ckanext.project.relationship_controller:Cadasta_Relationship_Controller'

        #project
        map.connect('project_surveys', '/project/{id}/surveys', controller=controller, action='show_surveys')
        map.connect('project_parcels', '/project/{id}/parcels', controller=parcel_controller, action='read_parcels')
        map.connect('project_resources', '/project/{id}/resources', controller=controller, action='read_resources')
        map.connect('project_activity_stream', '/project/{id}/activity_stream', controller=controller, action='read_activity_stream')
        map.connect('project_survey_template', '/project/{id}/survey_template', controller=controller, action='read_survey_template')
        map.connect('project_form_data_upload', '/project/{id}/form_data_upload', controller=controller, action='form_data_upload')

        #big map
        #map.connect('project_map', '/project/{id}/map', controller=map_controller, action='show_map')
        map.connect('edit_project_map', '/project/{id}/edit_map', controller=map_controller, action='edit_map')
        map.connect('show_parcel_map', '/project/{id}/parcel/{parcel_id}/map', controller=map_controller, action='show_parcel_map')
        map.connect('edit_parcel_map', '/project/{id}/edit_parcel/{parcel_id}/map', controller=map_controller, action='edit_parcel_map')
        map.connect('new_parcel_map', '/project/{id}/new/parcel/map', controller=map_controller, action='new_parcel_map')

        map.connect('show_relationship_map', '/project/{id}/parcel/{parcel_id}/relationship/{relationship_id}/map', controller=relationship, action='show_relationship_map')
        map.connect('edit_relationship_map', '/project/{id}/parcel/{parcel_id}/edit_relationship/{relationship_id}/map', controller=relationship, action='edit_relationship_map')
        map.connect('new_relationships_map', '/project/{id}/parcel/{parcel_id}/new/relationship/map', controller=relationship, action='new_relationship_map')

        #parcels
        map.connect('parcel_details', '/project/{id}/parcel/{parcel_id}', controller=parcel_controller, action='read_parcel_details')
        map.connect('edit_parcel_details', '/project/{id}/edit_parcel/{parcel_id}', controller=parcel_controller, action='edit_parcel_details')
        map.connect('new_parcel', '/project/{id}/new/parcel', controller=parcel_controller, action='new_parcel')

        #surveys
        map.connect('survey_details', '/project/{id}/survey/{survey_id}', controller=controller, action='read_survey_details')
        map.connect('edit_survey_details', '/project/{id}/edit_survey/{survey_id}', controller=controller, action='edit_survey_details')

        #relationship
        map.connect('relationship_details', '/project/{id}/parcel/{parcel_id}/relationship/{relationship_id}', controller=relationship, action='read_relationship_details')
        map.connect('edit_relationship_details', '/project/{id}/parcel/{parcel_id}/edit_relationship/{relationship_id}', controller=relationship, action='edit_relationship_details')
        map.connect('new_relationship', '/project/{id}/parcel/{parcel_id}/new/relationship', controller=relationship, action='new_relationship')
        map.connect('relationship_history', '/project/{id}/parcel/{parcel_id}/relationship_history', controller=relationship, action='get_relationship_history')

        #people/parties
        map.connect('party', '/project/{id}/party/{party_id}', controller=controller, action='read_party_details')
        map.connect('edit_party_details', '/project/{id}/edit_party/{party_id}', controller=controller, action='edit_party_details')
        map.connect('new_party', '/project/{id}/new/party', controller=controller, action='new_party')

        return map

    # IAuthFunctions
    def get_auth_functions(self):
        return {}

    # IActions
    def get_actions(self):
        return dict((name, function) for name, function
                    in action.__dict__.items()
                    if callable(function))

    # IPackageController
    def after_delete(self, context, package_dict):
        project_schema.project_archive(package_dict,context)

    def after_search(self, search_results, search_params):
        return search_results

    def after_show(self, context, package_dict):
        pass

    def after_update(self, context, package_dict):
        pass

    def after_create(self, context, package_dict):
        pass

    def before_search(self, search_params):
        return search_params

    def before_view(self, package_dict):
        return package_dict

    def before_index(self,package_dict):
        return package_dict

    def read(self, entity):
        pass

    def create(self, entity):
        pass

    def edit(self, entity):
        pass

    def authz_add_role(self, object_role):
        pass

    def authz_remove_role(self, object_role):
        pass

    def delete(self, entity):
        pass

<|endoftext|>


<commit_before>import os
import mimetypes
import paste.fileapp
import pylons.config as config

import ckan.logic as logic
import ckan.lib.base as base
import ckan.model as model
import ckan.lib.uploader as uploader
from ckan.common import _, request, c, response

from ckanext.s3filestore.uploader import S3Uploader

import logging
log = logging.getLogger(__name__)

NotFound = logic.NotFound
NotAuthorized = logic.NotAuthorized
get_action = logic.get_action
abort = base.abort
redirect = base.redirect

class S3Controller(base.BaseController):

    def resource_download(self, id, resource_id, filename=None):
        '''
        Provide a download by either redirecting the user to the url stored or
        downloading the uploaded file from S3.
        '''
        context = {'model': model, 'session': model.Session,
                   'user': c.user or c.author, 'auth_user_obj': c.userobj}

        try:
            rsc = get_action('resource_show')(context, {'id': resource_id})
            get_action('package_show')(context, {'id': id})
        except NotFound:
            abort(404, _('Resource not found'))
        except NotAuthorized:
            abort(401, _('Unauthorized to read resource %s') % id)

        if rsc.get('url_type') == 'upload':
            upload = uploader.get_resource_uploader(rsc)
            bucket_name = config.get('ckanext.s3filestore.aws_bucket_name')
            bucket = upload.get_s3_bucket(bucket_name)

            filename = os.path.basename(rsc['url'])
            key_path = upload.get_path(rsc['id'], filename)

            try:
                key = bucket.get_key(key_path)
            except Exception as e:
                raise e

            if key is None:
                abort(404, _('Resource data not found'))
            contents = key.get_contents_as_string()
            key.close()

            dataapp = paste.fileapp.DataApp(contents)

            try:
                status, headers, app_iter = request.call_application(dataapp)
            except OSError:
                abort(404, _('Resource data not found'))

            response.headers.update(dict(headers))
            response.status = status
            content_type, x = mimetypes.guess_type(rsc.get('url', ''))
            if content_type:
                response.headers['Content-Type'] = content_type
            return app_iter

        elif 'url' not in rsc:
            abort(404, _('No download is available'))
        redirect(rsc['url'])

    def group_image_redirect(self, filename):
        '''Redirect static group image requests to their location on S3.'''
        storage_path = S3Uploader.get_storage_path('group')
        filepath = os.path.join(storage_path, filename)
        redirect_url = 'https://{bucket_name}.s3.amazonaws.com/{filepath}' \
            .format(bucket_name=config.get('ckanext.s3filestore.aws_bucket_name'),
                    filepath=filepath)
        redirect(redirect_url)
<commit_msg>Don't always have to infer filename<commit_after>import os
import mimetypes
import paste.fileapp
import pylons.config as config

import ckan.logic as logic
import ckan.lib.base as base
import ckan.model as model
import ckan.lib.uploader as uploader
from ckan.common import _, request, c, response

from ckanext.s3filestore.uploader import S3Uploader

import logging
log = logging.getLogger(__name__)

NotFound = logic.NotFound
NotAuthorized = logic.NotAuthorized
get_action = logic.get_action
abort = base.abort
redirect = base.redirect

class S3Controller(base.BaseController):

    def resource_download(self, id, resource_id, filename=None):
        '''
        Provide a download by either redirecting the user to the url stored or
        downloading the uploaded file from S3.
        '''
        context = {'model': model, 'session': model.Session,
                   'user': c.user or c.author, 'auth_user_obj': c.userobj}

        try:
            rsc = get_action('resource_show')(context, {'id': resource_id})
            get_action('package_show')(context, {'id': id})
        except NotFound:
            abort(404, _('Resource not found'))
        except NotAuthorized:
            abort(401, _('Unauthorized to read resource %s') % id)

        if rsc.get('url_type') == 'upload':
            upload = uploader.get_resource_uploader(rsc)
            bucket_name = config.get('ckanext.s3filestore.aws_bucket_name')
            bucket = upload.get_s3_bucket(bucket_name)

            if filename is None:
                filename = os.path.basename(rsc['url'])
            key_path = upload.get_path(rsc['id'], filename)

            try:
                key = bucket.get_key(key_path)
            except Exception as e:
                raise e

            if key is None:
                abort(404, _('Resource data not found'))
            contents = key.get_contents_as_string()
            key.close()

            dataapp = paste.fileapp.DataApp(contents)

            try:
                status, headers, app_iter = request.call_application(dataapp)
            except OSError:
                abort(404, _('Resource data not found'))

            response.headers.update(dict(headers))
            response.status = status
            content_type, x = mimetypes.guess_type(rsc.get('url', ''))
            if content_type:
                response.headers['Content-Type'] = content_type
            return app_iter

        elif 'url' not in rsc:
            abort(404, _('No download is available'))
        redirect(rsc['url'])

    def group_image_redirect(self, filename):
        '''Redirect static group image requests to their location on S3.'''
        storage_path = S3Uploader.get_storage_path('group')
        filepath = os.path.join(storage_path, filename)
        redirect_url = 'https://{bucket_name}.s3.amazonaws.com/{filepath}' \
            .format(bucket_name=config.get('ckanext.s3filestore.aws_bucket_name'),
                    filepath=filepath)
        redirect(redirect_url)
<|endoftext|>