#!/usr/share/ucs-test/runner python
## desc: Check if App .ini has values in settings that are present.
## roles-not: [basesystem]
## tags: [basic, apptest]
## bugs: [33902]
## packages:
##   - univention-management-console-module-appcenter
## exposure: safe

from os import path
from glob import glob
from ConfigParser import RawConfigParser, Error, NoOptionError

import univention.testing.utils as utils
from univention.management.console.modules.appcenter.app_center import CACHE_DIR

OPTIONS_TO_CHECK = ('Contact', 'NotificationEmail')
failures_counter = 0


def fail_the_test():
    """
    Simply increases the global failures counter.
    """
    global failures_counter
    failures_counter += 1


def check_file(filename):
    """
    Creates an instance of RawConfigParser, reads a given 'filename' and
    checks if any option from the 'OPTIONS_TO_CHECK' has empty value, i.e:
    test fails if Contact='', but it won't fail if there is no Contact
    option found in the .ini file. Also fails if there is no '@' sign found.
    """
    print "\nChecking file '%s':" % filename
    IniConfig = RawConfigParser()
    IniConfig.read(filename)

    for option in OPTIONS_TO_CHECK:
        # it is allowed to have no 'option' in the .ini file.
        if IniConfig.has_option('Application', option):
            # if an 'option' is present - it should have a value assigned:
            option_value = IniConfig.get('Application', option)

            if not option_value:
                # 'option' has no value assigned, a failure case:
                fail_the_test()
                print("FAIL: the '%s' option for '%s' file exists, but has"
                      " no value assigned." % (option, filename))
            elif '@' not in option_value:
                # 'option' has no '@' sign in its value,
                # possibly an incorrect e-mail address was specified:
                fail_the_test()
                print("FAIL: the '@' sign for the '%s' option in file '%s'"
                      " was not found. Probably incorrect e-mail address: '%s'"
                      % (option, filename, option_value))


def check_ini_files():
    """
    Determines the path to App Center Cache dir with .ini files and checks
    all .inis there one by one.
    """
    print "\nThe path to Appcenter .ini files is:", CACHE_DIR
    if not path.exists(CACHE_DIR):
        utils.fail("The path to App center .ini files does not exist.")

    test_path = glob(path.join(CACHE_DIR, '*.ini'))
    for filename in test_path:
        try:
            check_file(filename)
        except Error as exc:
            fail_the_test()
            print("\nAn error ocured while trying to check the '%s' .ini "
                  "file. Error: %r" % (filename, exc))


if __name__ == '__main__':
    # find and check all .ini files:
    check_ini_files()

    if failures_counter:
        utils.fail("\nThere were %d problem(s) detected in the .ini files, "
                   "please check a complete test output." % failures_counter)
    else:
        print "\nNo errors were detected.\n"
