Skip to content

Commit

Permalink
Add offline url validation when auth type is offline (#49)
Browse files Browse the repository at this point in the history
* Add offline url validation for offline auth type

Signed-off-by: Mehdy Khoshnoody <[email protected]>

* Add tests for offline url validation

Signed-off-by: Mehdy Khoshnoody <[email protected]>
  • Loading branch information
mehdy authored Aug 31, 2020
1 parent b109a2b commit 30cd6ad
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
13 changes: 13 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"fmt"
"io/ioutil"
"net/url"
"os"
"regexp"
"strings"
Expand Down Expand Up @@ -82,6 +83,10 @@ func (l *Local) Validate() []error {
errors = append(errors, err)
}

if err := l.validateAuthOfflineURL(); err != nil {
errors = append(errors, err)
}

return errors
}

Expand All @@ -95,6 +100,14 @@ func (l *Local) validateIssueTracker() error {
l.IssueTracker, validIssueTrackers)
}

func (l *Local) validateAuthOfflineURL() error {
if _, err := url.ParseRequestURI(l.Auth.OfflineURL); l.Auth.Type == AuthTypeOffline && err != nil {
return fmt.Errorf("invalid offline URL: %q", l.Auth.OfflineURL)
}

return nil
}

func exists(filepath string) bool {
info, err := os.Stat(filepath)
if os.IsNotExist(err) {
Expand Down
5 changes: 5 additions & 0 deletions testing/test_configs/invalid_offline_url.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
origin: http://127.0.0.1:51596
issue_tracker: JIRA
auth:
type: offline
offline_url: INVALID_URL
4 changes: 4 additions & 0 deletions testing/test_configs/non_existent_offline_url.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
origin: http://127.0.0.1:51596
issue_tracker: JIRA
auth:
type: offline
22 changes: 22 additions & 0 deletions testing/todocheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,28 @@ func TestInvalidIssueTracker(t *testing.T) {
}
}

func TestInvalidOfflineURL(t *testing.T) {
err := scenariobuilder.NewScenario().
WithBinary("../todocheck").
WithConfig("./test_configs/invalid_offline_url.yaml").
ExpectExecutionError().
Run()
if err != nil {
t.Errorf("%s", err)
}
}

func TestNonExistentOfflineURL(t *testing.T) {
err := scenariobuilder.NewScenario().
WithBinary("../todocheck").
WithConfig("./test_configs/non_existent_offline_url.yaml").
ExpectExecutionError().
Run()
if err != nil {
t.Errorf("%s", err)
}
}

func TestTraversingNonExistentDirectory(t *testing.T) {
err := scenariobuilder.NewScenario().
WithBinary("../todocheck").
Expand Down

0 comments on commit 30cd6ad

Please sign in to comment.