- 
                Notifications
    
You must be signed in to change notification settings  - Fork 6
 
infrahubctl repository add: username flag fix #172
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
                    Changes from 2 commits
      Commits
    
    
            Show all changes
          
          
            5 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      112b734
              
                Change username default from empty string to None and add testing aro…
              
              
                FragmentedPacket cea3940
              
                Fix multiline spacing.
              
              
                FragmentedPacket 0f0c908
              
                Removed commented out imports and remove os import per Guillaume's fe…
              
              
                FragmentedPacket 99e8433
              
                Remove fixtures dir variable as we aren't using it currently.
              
              
                FragmentedPacket 02d5b31
              
                Fix linting/formatting.
              
              
                FragmentedPacket File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,287 @@ | ||
| """Integration tests for infrahubctl commands.""" | ||
| 
     | 
||
| # import json | ||
| import os | ||
| 
     | 
||
| # import sys | ||
                
      
                  FragmentedPacket marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| from pathlib import Path | ||
| from unittest import mock | ||
| 
     | 
||
| import pytest | ||
| 
     | 
||
| # from pytest_httpx._httpx_mock import HTTPXMock | ||
| from typer.testing import CliRunner | ||
| 
     | 
||
| from infrahub_sdk.client import InfrahubClient | ||
| from infrahub_sdk.ctl.cli_commands import app | ||
| 
     | 
||
| # from tests.helpers.utils import change_directory, strip_color | ||
| 
     | 
||
| runner = CliRunner() | ||
| 
     | 
||
| 
     | 
||
| FIXTURE_BASE_DIR = Path( | ||
| Path(os.path.abspath(__file__)).parent / ".." / ".." / "fixtures" / "integration" / "test_infrahubctl" | ||
                
      
                  FragmentedPacket marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| ) | ||
| 
     | 
||
| 
     | 
||
| @pytest.fixture | ||
| def mock_client() -> mock.Mock: | ||
| """Fixture for a mocked InfrahubClient.""" | ||
| client = mock.create_autospec(InfrahubClient) | ||
| return client | ||
| 
     | 
||
| 
     | 
||
| # --------------------------------------------------------- | ||
| # infrahubctl repository command tests | ||
| # --------------------------------------------------------- | ||
| @mock.patch("infrahub_sdk.ctl.repository.initialize_client") | ||
| class TestInfrahubctlRepository: | ||
| """Groups the 'infrahubctl repository' test cases.""" | ||
| 
     | 
||
| def test_repo_no_username(self, mock_init_client, mock_client) -> None: | ||
| """Case allow no username to be passed in and set it as None rather than blank string that fails.""" | ||
| mock_cred = mock.AsyncMock() | ||
| mock_cred.id = "1234" | ||
| mock_client.create.return_value = mock_cred | ||
| 
     | 
||
| mock_init_client.return_value = mock_client | ||
| output = runner.invoke( | ||
| app, | ||
| [ | ||
| "repository", | ||
| "add", | ||
| "Gitlab", | ||
| "https://gitlab.com/FragmentedPacket/nautobot-plugin-ansible-filters.git", | ||
| "--password", | ||
| "mySup3rSecureP@ssw0rd", | ||
| ], | ||
| ) | ||
| assert output.exit_code == 0 | ||
| mock_client.create.assert_called_once() | ||
| mock_client.create.assert_called_with( | ||
| name="Gitlab", | ||
| kind="CorePasswordCredential", | ||
| password="mySup3rSecureP@ssw0rd", | ||
| username=None, | ||
| ) | ||
| mock_cred.save.assert_called_once() | ||
| mock_cred.save.assert_called_with(allow_upsert=True) | ||
| mock_client.execute_graphql.assert_called_once() | ||
| mock_client.execute_graphql.assert_called_with( | ||
| query=""" | ||
| mutation { | ||
| CoreRepositoryCreate( | ||
| data: { | ||
| name: { | ||
| value: "Gitlab" | ||
| } | ||
| location: { | ||
| value: "https://gitlab.com/FragmentedPacket/nautobot-plugin-ansible-filters.git" | ||
| } | ||
| description: { | ||
| value: "" | ||
| } | ||
| commit: { | ||
| value: "" | ||
| } | ||
| credential: { | ||
| id: "1234" | ||
| } | ||
| } | ||
| ){ | ||
| ok | ||
| } | ||
| } | ||
| """, | ||
| branch_name="main", | ||
| tracker="mutation-repository-create", | ||
| ) | ||
| 
     | 
||
| def test_repo_username(self, mock_init_client, mock_client) -> None: | ||
| """Case allow no username to be passed in and set it as None rather than blank string that fails.""" | ||
| mock_cred = mock.AsyncMock() | ||
| mock_cred.id = "1234" | ||
| mock_client.create.return_value = mock_cred | ||
| 
     | 
||
| mock_init_client.return_value = mock_client | ||
| output = runner.invoke( | ||
| app, | ||
| [ | ||
| "repository", | ||
| "add", | ||
| "Gitlab", | ||
| "https://gitlab.com/FragmentedPacket/nautobot-plugin-ansible-filters.git", | ||
| "--password", | ||
| "mySup3rSecureP@ssw0rd", | ||
| "--username", | ||
| "opsmill", | ||
| ], | ||
| ) | ||
| assert output.exit_code == 0 | ||
| mock_client.create.assert_called_once() | ||
| mock_client.create.assert_called_with( | ||
| name="Gitlab", | ||
| kind="CorePasswordCredential", | ||
| password="mySup3rSecureP@ssw0rd", | ||
| username="opsmill", | ||
| ) | ||
| mock_cred.save.assert_called_once() | ||
| mock_cred.save.assert_called_with(allow_upsert=True) | ||
| mock_client.execute_graphql.assert_called_once() | ||
| mock_client.execute_graphql.assert_called_with( | ||
| query=""" | ||
| mutation { | ||
| CoreRepositoryCreate( | ||
| data: { | ||
| name: { | ||
| value: "Gitlab" | ||
| } | ||
| location: { | ||
| value: "https://gitlab.com/FragmentedPacket/nautobot-plugin-ansible-filters.git" | ||
| } | ||
| description: { | ||
| value: "" | ||
| } | ||
| commit: { | ||
| value: "" | ||
| } | ||
| credential: { | ||
| id: "1234" | ||
| } | ||
| } | ||
| ){ | ||
| ok | ||
| } | ||
| } | ||
| """, | ||
| branch_name="main", | ||
| tracker="mutation-repository-create", | ||
| ) | ||
| 
     | 
||
| def test_repo_readonly_true(self, mock_init_client, mock_client) -> None: | ||
| """Case allow no username to be passed in and set it as None rather than blank string that fails.""" | ||
| mock_cred = mock.AsyncMock() | ||
| mock_cred.id = "1234" | ||
| mock_client.create.return_value = mock_cred | ||
| 
     | 
||
| mock_init_client.return_value = mock_client | ||
| output = runner.invoke( | ||
| app, | ||
| [ | ||
| "repository", | ||
| "add", | ||
| "Gitlab", | ||
| "https://gitlab.com/FragmentedPacket/nautobot-plugin-ansible-filters.git", | ||
| "--password", | ||
| "mySup3rSecureP@ssw0rd", | ||
| "--read-only", | ||
| ], | ||
| ) | ||
| assert output.exit_code == 0 | ||
| mock_client.create.assert_called_once() | ||
| mock_client.create.assert_called_with( | ||
| name="Gitlab", | ||
| kind="CorePasswordCredential", | ||
| password="mySup3rSecureP@ssw0rd", | ||
| username=None, | ||
| ) | ||
| mock_cred.save.assert_called_once() | ||
| mock_cred.save.assert_called_with(allow_upsert=True) | ||
| mock_client.execute_graphql.assert_called_once() | ||
| mock_client.execute_graphql.assert_called_with( | ||
| query=""" | ||
| mutation { | ||
| CoreReadOnlyRepositoryCreate( | ||
| data: { | ||
| name: { | ||
| value: "Gitlab" | ||
| } | ||
| location: { | ||
| value: "https://gitlab.com/FragmentedPacket/nautobot-plugin-ansible-filters.git" | ||
| } | ||
| description: { | ||
| value: "" | ||
| } | ||
| commit: { | ||
| value: "" | ||
| } | ||
| credential: { | ||
| id: "1234" | ||
| } | ||
| } | ||
| ){ | ||
| ok | ||
| } | ||
| } | ||
| """, | ||
| branch_name="main", | ||
| tracker="mutation-repository-create", | ||
| ) | ||
| 
     | 
||
| def test_repo_description_commit_branch(self, mock_init_client, mock_client) -> None: | ||
| """Case allow no username to be passed in and set it as None rather than blank string that fails.""" | ||
| mock_cred = mock.AsyncMock() | ||
| mock_cred.id = "1234" | ||
| mock_client.create.return_value = mock_cred | ||
| 
     | 
||
| mock_init_client.return_value = mock_client | ||
| output = runner.invoke( | ||
| app, | ||
| [ | ||
| "repository", | ||
| "add", | ||
| "Gitlab", | ||
| "https://gitlab.com/FragmentedPacket/nautobot-plugin-ansible-filters.git", | ||
| "--password", | ||
| "mySup3rSecureP@ssw0rd", | ||
| "--username", | ||
| "opsmill", | ||
| "--description", | ||
| "This is a test description", | ||
| "--commit", | ||
| "myHashCommit", | ||
| "--branch", | ||
| "develop", | ||
| ], | ||
| ) | ||
| assert output.exit_code == 0 | ||
| mock_client.create.assert_called_once() | ||
| mock_client.create.assert_called_with( | ||
| name="Gitlab", | ||
| kind="CorePasswordCredential", | ||
| password="mySup3rSecureP@ssw0rd", | ||
| username="opsmill", | ||
| ) | ||
| mock_cred.save.assert_called_once() | ||
| mock_cred.save.assert_called_with(allow_upsert=True) | ||
| mock_client.execute_graphql.assert_called_once() | ||
| mock_client.execute_graphql.assert_called_with( | ||
| query=""" | ||
| mutation { | ||
| CoreRepositoryCreate( | ||
| data: { | ||
| name: { | ||
| value: "Gitlab" | ||
| } | ||
| location: { | ||
| value: "https://gitlab.com/FragmentedPacket/nautobot-plugin-ansible-filters.git" | ||
| } | ||
| description: { | ||
| value: "This is a test description" | ||
| } | ||
| commit: { | ||
| value: "myHashCommit" | ||
| } | ||
| credential: { | ||
| id: "1234" | ||
| } | ||
| } | ||
| ){ | ||
| ok | ||
| } | ||
| } | ||
| """, | ||
| branch_name="develop", | ||
| tracker="mutation-repository-create", | ||
| ) | ||
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.