Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add plumbing for passing credentials to devices #507

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

Merged
merged 8 commits into from
Sep 13, 2023

Conversation

sdb9696
Copy link
Collaborator

@sdb9696 sdb9696 commented Aug 31, 2023

Add credentials classes and cli support to support future implementation of authenticating protocols. See #477 for discussion

Copy link
Member

@rytilahti rytilahti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! I think the smartdevice ctor change should also go into this PR, as that will allow writing some unittests to make sure it's all working as expected.

@sdb9696 sdb9696 changed the title Add plumbing for passing credentials as far as discovery Add plumbing for passing credentials to devices Sep 4, 2023
@sdb9696
Copy link
Collaborator Author

sdb9696 commented Sep 4, 2023

Looks good to me! I think the smartdevice ctor change should also go into this PR, as that will allow writing some unittests to make sure it's all working as expected.

Now done. Also changed the default of username and password to None and removed the check in discovery for 255 as per comments on #477. Also had to tweak the cli handling of the json parameter because tests failed if they appeared after the test_json_output because echo remained a noop

@codecov
Copy link

codecov bot commented Sep 4, 2023

Codecov Report

Patch coverage is 97.95% of modified lines.

Files Changed Coverage
kasa/cli.py 93.75%
kasa/__init__.py 100.00%
kasa/credentials.py 100.00%
kasa/discover.py 100.00%
kasa/exceptions.py 100.00%
kasa/smartbulb.py 100.00%
kasa/smartdevice.py 100.00%
kasa/smartdimmer.py 100.00%
kasa/smartlightstrip.py 100.00%
kasa/smartplug.py 100.00%
... and 1 more

📢 Thoughts on this report? Let us know!.

@sdb9696 sdb9696 requested a review from rytilahti September 4, 2023 11:57
@sdb9696
Copy link
Collaborator Author

sdb9696 commented Sep 11, 2023

Looks good to me! I think the smartdevice ctor change should also go into this PR, as that will allow writing some unittests to make sure it's all working as expected.

Now done. Also changed the default of username and password to None and removed the check in discovery for 255 as per comments on #477. Also had to tweak the cli handling of the json parameter because tests failed if they appeared after the test_json_output because echo remained a noop

Hi @rytilahti, any thoughts on the latest here? It should have everything you wanted now.

Copy link
Member

@rytilahti rytilahti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the ping, I've been busy and forgot to reply here earlier. Would you mind adding a parametrized test for the constructors of smartdevice classes to verify that the input parameters are stored as expected?

@sdb9696
Copy link
Collaborator Author

sdb9696 commented Sep 12, 2023

Thanks for the ping, I've been busy and forgot to reply here earlier. Would you mind adding a parametrized test for the constructors of smartdevice classes to verify that the input parameters are stored as expected?

I think I have done this by passing in all the discovery fixtures and calling cli with the --type parameter.

@sdb9696 sdb9696 requested a review from rytilahti September 12, 2023 17:20
@rytilahti
Copy link
Member

I think I have done this by passing in all the discovery fixtures and calling cli with the --type parameter.

It's more important to test against the API as that's likely more consumed by external users than the cli tool. Tests for both are obviously better, but the API takes priority.

@sdb9696
Copy link
Collaborator Author

sdb9696 commented Sep 12, 2023

I think I have done this by passing in all the discovery fixtures and calling cli with the --type parameter.

It's more important to test against the API as that's likely more consumed by external users than the cli tool. Tests for both are obviously better, but the API takes priority.

Ok, so a test that creates an instance itself and then checks the credentials are there? With parameters of all the possible derived classes of SmartDevice?

@rytilahti
Copy link
Member

Ok, so a test that creates an instance itself and then checks the credentials are there? With parameters of all the possible derived classes of SmartDevice?

Something like this should suffice, the idea is just to avoid breaking that API by chance:

@pytest.mark.parametrize("cls", [SmartDevice, SmartBulb, ...])
def test_device_class_ctors(cls): 
    host = "127.0.0.1"
    port = 1234
    credentials = ...
    dev = cls(host, port=port, credentials=credentials)
    assert dev.host = host
    assert dev.port = port
    assert dev.credentials = credentials

I'm not sure which file it should go to, though...

@sdb9696
Copy link
Collaborator Author

sdb9696 commented Sep 13, 2023

Ok, so a test that creates an instance itself and then checks the credentials are there? With parameters of all the possible derived classes of SmartDevice?

Something like this should suffice, the idea is just to avoid breaking that API by chance:

@pytest.mark.parametrize("cls", [SmartDevice, SmartBulb, ...])
def test_device_class_ctors(cls): 
    host = "127.0.0.1"
    port = 1234
    credentials = ...
    dev = cls(host, port=port, credentials=credentials)
    assert dev.host = host
    assert dev.port = port
    assert dev.credentials = credentials

I'm not sure which file it should go to, though...

Ok so added this test to test_smartdevice.py.

@sdb9696
Copy link
Collaborator Author

sdb9696 commented Sep 13, 2023

Do you mean override it for every test other than json? The issue here is that if any test runs after the json test then the global echo is still set to noop. It just happens I'm the first person to add a test after the json test. (I guess this could affect any other code calling the cli module directly but not sure if that's a use case)

Another way to do it without introducing another global could be to introduce a function that gets the echo function:

try:
    from rich import print as rich_print
except ImportError:
    pass

def _get_echo_function():
    def _strip_rich_formatting(echo_func):

        @wraps(echo_func)
        def wrapper(message=None, *args, **kwargs):
            if message is not None:
                message = re.sub(r"\[/?.+?]", "", message)
            echo_func(message, *args, **kwargs)

        return wrapper

    return rich_print if rich_print else _strip_rich_formatting(click.echo)

Then just:

    # If JSON output is requested, disable echo
    global echo
    if json:

        def _nop_echo(*args, **kwargs):
            pass

        echo = _nop_echo
    else:
        # Set back to default is required if running tests with CliRunner
        echo = _get_echo_function()

@rytilahti rytilahti added the enhancement New feature or request label Sep 13, 2023
@rytilahti
Copy link
Member

rytilahti commented Sep 13, 2023

I don't currently have the energy nor the time to dig into that, so let's just add a comment, keep it as it is for now and let's get this merged (after fixing the test that does not really test the real output) :-)

@sdb9696
Copy link
Collaborator Author

sdb9696 commented Sep 13, 2023

I don't currently have the energy nor the time to dig into that, so let's just add a comment, keep it as it is for now and let's get this merged (after fixing the test that does not really test the real output) :-)

That's done. Re: the test that doesn't check the real output it does check that credentials when properly supplied are passed from the cli to the device so I think this should be good to go

@rytilahti rytilahti merged commit 7bb4a45 into python-kasa:master Sep 13, 2023
@sdb9696 sdb9696 mentioned this pull request Sep 14, 2023
@sdb9696 sdb9696 deleted the add_credentials_support branch December 6, 2023 19:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants