-
Notifications
You must be signed in to change notification settings - Fork 0
ci: improve test coverage #25
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 all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,9 +5,6 @@ Copyright © 2025 Zeno Belli [email protected] | |
| package cmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/spf13/viper" | ||
| "github.com/xenos76/https-wrench/internal/certinfo" | ||
|
|
@@ -48,14 +45,14 @@ Examples: | |
| https-wrench certinfo --ca-bundle ./ca-bundle.pem --tls-endpoint example.com:443 | ||
| https-wrench certinfo --ca-bundle ./ca-bundle.pem --cert-bundle ./bundle.pem --key-file ./key.pem | ||
| `, | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| Run: func(cmd *cobra.Command, _ []string) { | ||
| caBundleValue := viper.GetString("ca-bundle") | ||
| certBundleValue := viper.GetString("cert-bundle") | ||
| keyFileValue := viper.GetString("key-file") | ||
| versionRequested := viper.GetBool("version") | ||
|
|
||
| if versionRequested { | ||
| fmt.Print(version) | ||
| cmd.Print(version) | ||
| return | ||
| } | ||
|
|
||
|
|
@@ -67,16 +64,16 @@ Examples: | |
|
|
||
| certinfoCfg, err := certinfo.NewCertinfoConfig() | ||
| if err != nil { | ||
| fmt.Printf("Error creating new Certinfo config: %s", err) | ||
| cmd.Printf("Error creating new Certinfo config: %s", err) | ||
| return | ||
| } | ||
|
|
||
| if err = certinfoCfg.SetCaPoolFromFile(caBundleValue, fileReader); err != nil { | ||
| fmt.Printf("Error importing CA Certificate bundle from file: %s", err) | ||
| cmd.Printf("Error importing CA Certificate bundle from file: %s", err) | ||
| } | ||
|
|
||
| if err = certinfoCfg.SetCertsFromFile(certBundleValue, fileReader); err != nil { | ||
| fmt.Printf("Error importing Certificate bundle from file: %s", err) | ||
| cmd.Printf("Error importing Certificate bundle from file: %s", err) | ||
| } | ||
|
|
||
| certinfoCfg.SetTLSInsecure(tlsInsecure).SetTLSServerName(tlsServerName) | ||
|
|
@@ -85,20 +82,20 @@ Examples: | |
| // before being able to ask details about the certificate we want to a | ||
| // webserver using self-signed and valid certificates | ||
| if err = certinfoCfg.SetTLSEndpoint(tlsEndpoint); err != nil { | ||
| fmt.Printf("Error setting TLS endpoint: %s", err) | ||
| cmd.Printf("Error setting TLS endpoint: %s", err) | ||
| } | ||
|
|
||
| if err = certinfoCfg.SetPrivateKeyFromFile( | ||
| keyFileValue, | ||
| keyPwEnvVar, | ||
| fileReader, | ||
| ); err != nil { | ||
| fmt.Printf("Error importing key from file: %s", err) | ||
| cmd.Printf("Error importing key from file: %s", err) | ||
| } | ||
|
|
||
| // dump.Print(certinfoCfg) | ||
| if err = certinfoCfg.PrintData(os.Stdout); err != nil { | ||
| fmt.Printf("error printing Certinfo data: %s", err) | ||
| if err = certinfoCfg.PrintData(cmd.OutOrStdout()); err != nil { | ||
| cmd.Printf("error printing Certinfo data: %s", err) | ||
| } | ||
| }, | ||
| } | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -5,10 +5,8 @@ Copyright © 2026 Zeno Belli <[email protected]> | |
| package cmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "os" | ||
|
|
||
| "github.com/MicahParks/keyfunc/v3" | ||
| "github.com/spf13/cobra" | ||
|
|
@@ -31,8 +29,8 @@ var ( | |
|
|
||
| var jwtinfoCmd = &cobra.Command{ | ||
| Use: "jwtinfo", | ||
| Short: "JwtInfo request and display JWT token data", | ||
| Long: `JwtInfo request and display JWT token data | ||
| Short: "JwtInfo shows data from a JWT token", | ||
| Long: `JwtInfo shows data from a JWT token | ||
|
|
||
| Examples: | ||
| export REQ_URL="https://sample.provider/oauth/token" | ||
|
|
@@ -51,7 +49,7 @@ Examples: | |
| # Request and validate a JWT token | ||
| https-wrench jwtinfo --request-url $REQ_URL --request-values-json $REQ_VALUES --validation-url $VALIDATION_URL | ||
| `, | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| Run: func(cmd *cobra.Command, _ []string) { | ||
| // TODO: remove global --config option | ||
|
|
||
| var err error | ||
|
|
@@ -60,7 +58,7 @@ Examples: | |
| if tokenFile != "" { | ||
| tokenData, err = jwtinfo.ReadTokenFromFile(tokenFile) | ||
| if err != nil { | ||
| fmt.Printf( | ||
| cmd.Printf( | ||
| "error while reading token value from file: %s", | ||
| err, | ||
| ) | ||
|
|
@@ -78,7 +76,7 @@ Examples: | |
| requestValuesMap, | ||
| ) | ||
| if err != nil { | ||
| fmt.Printf( | ||
| cmd.Printf( | ||
| "error while reading request's values from file: %s", | ||
| err, | ||
| ) | ||
|
|
@@ -92,7 +90,7 @@ Examples: | |
| requestValuesMap, | ||
| ) | ||
| if err != nil { | ||
| fmt.Printf( | ||
| cmd.Printf( | ||
| "error while parsing request's values JSON string: %s", | ||
| err, | ||
| ) | ||
|
|
@@ -107,29 +105,29 @@ Examples: | |
| io.ReadAll, | ||
| ) | ||
| if err != nil { | ||
| fmt.Printf("error while requesting token data: %s\n", err) | ||
| cmd.Printf("error while requesting token data: %s\n", err) | ||
| return | ||
| } | ||
| } | ||
|
|
||
| if tokenData.AccessTokenRaw != "" { | ||
| err = tokenData.DecodeBase64() | ||
| if err != nil { | ||
| fmt.Printf("DecodeBase64 error: %s\n", err) | ||
| cmd.Printf("DecodeBase64 error: %s\n", err) | ||
| return | ||
| } | ||
|
|
||
| if jwksURL != "" { | ||
| err = tokenData.ParseWithJWKS(jwksURL, keyfuncDefOverride) | ||
| if err != nil { | ||
| fmt.Printf("error while parsing token data: %s\n", err) | ||
| cmd.Printf("error while parsing token data: %s\n", err) | ||
| return | ||
| } | ||
| } | ||
|
|
||
| err = jwtinfo.PrintTokenInfo(tokenData, os.Stdout) | ||
| err = jwtinfo.PrintTokenInfo(tokenData, cmd.OutOrStdout()) | ||
| if err != nil { | ||
| fmt.Printf("error while printing token data: %s\n", err) | ||
| cmd.Printf("error while printing token data: %s\n", err) | ||
| return | ||
| } | ||
| } else { | ||
|
|
||
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,58 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestJwtinfoCmd(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| args []string | ||
| expectError bool | ||
| errMsgs []string | ||
| expected []string | ||
| }{ | ||
| { | ||
| name: "invalid file", | ||
| args: []string{"jwtinfo", "--token-file", "non_existent.jwt"}, | ||
| expectError: false, | ||
| expected: []string{"error while reading token value from file"}, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| tt := tc | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Cleanup(func() { | ||
| rootCmd.Flags().Set("version", "false") | ||
| jwtinfoCmd.Flags().Set("token-file", "") | ||
| jwtinfoCmd.Flags().Set("clipboard", "false") | ||
| }) | ||
|
|
||
| reqOut := new(bytes.Buffer) | ||
| reqCmd := rootCmd | ||
| reqCmd.SetOut(reqOut) | ||
| reqCmd.SetErr(reqOut) | ||
| reqCmd.SetArgs(tt.args) | ||
| err := reqCmd.Execute() | ||
|
|
||
| if tt.expectError { | ||
| require.Error(t, err) | ||
|
|
||
| for _, expected := range tt.errMsgs { | ||
| require.ErrorContains(t, err, expected) | ||
| } | ||
| } else { | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| got := reqOut.String() | ||
| for _, expected := range tt.expected { | ||
| require.Contains(t, got, expected) | ||
| } | ||
| }) | ||
| } | ||
| } |
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
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
Oops, something went wrong.
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.