-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #271 from SomtochiAma/token-log
Use regex to find and replace token
- Loading branch information
Showing
2 changed files
with
70 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 server | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
) | ||
|
||
func TestRedactTokenFromError(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
token string | ||
originalErrStr string | ||
expectedErrStr string | ||
}{ | ||
{ | ||
name: "no token", | ||
token: "8h0387hdyehbwwa45", | ||
originalErrStr: "Cannot post to github", | ||
expectedErrStr: "Cannot post to github", | ||
}, | ||
{ | ||
name: "empty token", | ||
token: "", | ||
originalErrStr: "Cannot post to github", | ||
expectedErrStr: "Cannot post to github", | ||
}, | ||
{ | ||
name: "exact token", | ||
token: "8h0387hdyehbwwa45", | ||
originalErrStr: "Cannot post to github with token 8h0387hdyehbwwa45", | ||
expectedErrStr: "Cannot post to github with token *****", | ||
}, | ||
{ | ||
name: "non-exact token", | ||
token: "8h0387hdyehbwwa45", | ||
originalErrStr: `Cannot post to github with token 8h0387hdyehbwwa45\\n`, | ||
expectedErrStr: `Cannot post to github with token *****\\n`, | ||
}, | ||
{ | ||
name: "extra text in front token", | ||
token: "8h0387hdyehbwwa45", | ||
originalErrStr: `Cannot post to github with token metoo8h0387hdyehbwwa45\\n`, | ||
expectedErrStr: `Cannot post to github with token metoo*****\\n`, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
err := redactTokenFromError(errors.New(tt.originalErrStr), tt.token) | ||
if err == nil { | ||
t.Fatalf("error shouldn't be nil") | ||
} | ||
|
||
if err.Error() != tt.expectedErrStr { | ||
t.Errorf("expected error string '%s' but got '%s'", | ||
tt.expectedErrStr, err) | ||
} | ||
} | ||
} |