-
Notifications
You must be signed in to change notification settings - Fork 2
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 #5 from shogo82148/implement-github-token-validater
implement GitHub Token validator
- Loading branch information
Showing
2 changed files
with
226 additions
and
2 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,114 @@ | ||
package githubapptoken | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/shogo82148/actions-github-app-token/provider/github-app-token/github" | ||
) | ||
|
||
type githubClientMock struct { | ||
CreateStatusFunc func(ctx context.Context, token, owner, repo, ref string, status *github.CreateStatusRequest) (*github.CreateStatusResponse, error) | ||
} | ||
|
||
func (c *githubClientMock) CreateStatus(ctx context.Context, token, owner, repo, ref string, status *github.CreateStatusRequest) (*github.CreateStatusResponse, error) { | ||
return c.CreateStatusFunc(ctx, token, owner, repo, ref, status) | ||
} | ||
|
||
func TestValidateGitHubToken(t *testing.T) { | ||
h := &Handler{ | ||
github: &githubClientMock{ | ||
CreateStatusFunc: func(ctx context.Context, token, owner, repo, ref string, status *github.CreateStatusRequest) (*github.CreateStatusResponse, error) { | ||
if token != "ghs_dummyGitHubToken" { | ||
t.Errorf("unexpected GitHub Token: want %q, got %q", "ghs_dummyGitHubToken", token) | ||
} | ||
if owner != "fuller-inc" { | ||
t.Errorf("unexpected owner: want %q, got %q", "fuller-inc", owner) | ||
} | ||
if repo != "actions-aws-assume-role" { | ||
t.Errorf("unexpected repo: want %q, got %q", "actions-aws-assume-role", repo) | ||
} | ||
if ref != "e3a45c6c16c1464826b36a598ff39e6cc98c4da4" { | ||
t.Errorf("unexpected ref: want %q, got %q", "e3a45c6c16c1464826b36a598ff39e6cc98c4da4", ref) | ||
} | ||
if status.State != github.CommitStateSuccess { | ||
t.Errorf("unexpected commit status state: want %s, got %s", github.CommitStateSuccess, status.State) | ||
} | ||
if status.Context != commitStatusContext { | ||
t.Errorf("unexpected commit status context: want %q, got %q", commitStatusContext, status.Context) | ||
} | ||
return &github.CreateStatusResponse{ | ||
Creator: &github.CreateStatusResponseCreator{ | ||
Login: creatorLogin, | ||
ID: creatorID, | ||
Type: creatorType, | ||
}, | ||
}, nil | ||
}, | ||
}, | ||
} | ||
err := h.validateGitHubToken(context.Background(), &requestBody{ | ||
GitHubToken: "ghs_dummyGitHubToken", | ||
Repository: "fuller-inc/actions-aws-assume-role", | ||
SHA: "e3a45c6c16c1464826b36a598ff39e6cc98c4da4", | ||
}) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
} | ||
|
||
func TestValidateGitHubToken_PermissionError(t *testing.T) { | ||
h := &Handler{ | ||
github: &githubClientMock{ | ||
CreateStatusFunc: func(ctx context.Context, token, owner, repo, ref string, status *github.CreateStatusRequest) (*github.CreateStatusResponse, error) { | ||
return nil, &github.UnexpectedStatusCodeError{ | ||
StatusCode: http.StatusBadRequest, | ||
} | ||
}, | ||
}, | ||
} | ||
err := h.validateGitHubToken(context.Background(), &requestBody{ | ||
GitHubToken: "ghs_dummyGitHubToken", | ||
Repository: "fuller-inc/actions-aws-assume-role", | ||
SHA: "e3a45c6c16c1464826b36a598ff39e6cc98c4da4", | ||
}) | ||
if err == nil { | ||
t.Error("want error, but not") | ||
} | ||
|
||
var validate *validationError | ||
if !errors.As(err, &validate) { | ||
t.Errorf("want validation error, got %T", err) | ||
} | ||
} | ||
|
||
func TestValidateGitHubToken_InvalidCreator(t *testing.T) { | ||
h := &Handler{ | ||
github: &githubClientMock{ | ||
CreateStatusFunc: func(ctx context.Context, token, owner, repo, ref string, status *github.CreateStatusRequest) (*github.CreateStatusResponse, error) { | ||
return &github.CreateStatusResponse{ | ||
Creator: &github.CreateStatusResponseCreator{ | ||
Login: "shogo82148", | ||
ID: 1157344, | ||
Type: "User", | ||
}, | ||
}, nil | ||
}, | ||
}, | ||
} | ||
err := h.validateGitHubToken(context.Background(), &requestBody{ | ||
GitHubToken: "ghs_dummyGitHubToken", | ||
Repository: "fuller-inc/actions-aws-assume-role", | ||
SHA: "e3a45c6c16c1464826b36a598ff39e6cc98c4da4", | ||
}) | ||
if err == nil { | ||
t.Error("want error, but not") | ||
} | ||
|
||
var validate *validationError | ||
if !errors.As(err, &validate) { | ||
t.Errorf("want validation error, got %T", err) | ||
} | ||
} |