Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: require owner only if no owner_account is set #2114

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions sopel/config/core_section.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
ChoiceAttribute,
FilenameAttribute,
ListAttribute,
NO_DEFAULT,
SecretAttribute,
StaticSection,
ValidatedAttribute,
Expand Down Expand Up @@ -88,6 +87,17 @@ class CoreSection(StaticSection):
with the minimal required options.

"""
def __init__(self, config, section_name, validate=True):
super().__init__(config, section_name, validate)

if validate:
# `owner` is required UNLESS `owner_account` is set
owner = getattr(self, 'owner', None)
owner_account = getattr(self, 'owner_account', None)
if not any([owner, owner_account]):
raise ValueError(
'Either {sect}.owner or {sect}.owner_account is required'
.format(sect=section_name))

admins = ListAttribute('admins')
"""The list of people (other than the owner) who can administer the bot.
Expand Down Expand Up @@ -952,10 +962,10 @@ def homedir(self):
won't work until the bot has been properly configured.
"""

owner = ValidatedAttribute('owner', default=NO_DEFAULT)
owner = ValidatedAttribute('owner')
"""The IRC name of the owner of the bot.

**Required** even if :attr:`owner_account` is set.
Ignored if :attr:`owner_account` is set.
"""

owner_account = ValidatedAttribute('owner_account')
Expand Down
66 changes: 66 additions & 0 deletions test/config/test_config_core_section.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# coding=utf-8
from __future__ import absolute_import, division, print_function, unicode_literals

import pytest

from sopel import config


FAKE_OWNER_CONFIG = """
[core]
owner=dgw
"""

FAKE_OWNER_ACCOUNT_CONFIG = """
[core]
owner_account=dgw
"""

FAKE_NO_OWNER_CONFIG = """
[core]
"""

FAKE_EMPTY_OWNER_CONFIG = """
[core]
owner=
"""

FAKE_EMPTY_OWNER_ACCOUNT_CONFIG = """
[core]
owner_account=
"""


@pytest.fixture
def tmphomedir(tmpdir):
sopel_homedir = tmpdir.join('.sopel')
sopel_homedir.mkdir()
sopel_homedir.join('test.tmp').write('')
sopel_homedir.join('test.d').mkdir()
return sopel_homedir


def get_fake_config(contents, tmphomedir):
conf_file = tmphomedir.join('conf.cfg')
conf_file.write(contents)

test_settings = config.Config(conf_file.strpath)
return test_settings


@pytest.mark.parametrize('contents', [
FAKE_OWNER_CONFIG,
FAKE_OWNER_ACCOUNT_CONFIG,
])
def test_core_section_owner_present(contents, tmphomedir):
get_fake_config(contents, tmphomedir)


@pytest.mark.parametrize('contents', [
FAKE_NO_OWNER_CONFIG,
FAKE_EMPTY_OWNER_CONFIG,
FAKE_EMPTY_OWNER_ACCOUNT_CONFIG,
])
def test_core_section_owner_missing(contents, tmphomedir):
with pytest.raises(ValueError):
get_fake_config(contents, tmphomedir)