-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(oauth): scope test: add t.Parallel() feat: add oauth module
- Loading branch information
1 parent
bd2de0d
commit 869811a
Showing
11 changed files
with
1,641 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
--- | ||
no-hard-tabs: false | ||
no-duplicate-heading: false |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,96 @@ | ||
// Package oauth ... | ||
package oauth // import "github.com/SevereCloud/vksdk/v2/api/oauth" | ||
|
||
import "errors" | ||
|
||
// ValidationType ... | ||
type ValidationType string | ||
|
||
// Possible values. | ||
const ( | ||
ValidationSMS ValidationType = "2fa_sms" | ||
ValidationApp ValidationType = "2fa_app" | ||
) | ||
|
||
// ErrorType for oauth. | ||
type ErrorType string | ||
|
||
// Error types. | ||
// | ||
// See https://tools.ietf.org/html/rfc6749#section-4.2.2.1 | ||
const ( | ||
ErrInvalidRequest ErrorType = "invalid_request" | ||
ErrUnauthorizedClient ErrorType = "unauthorized_client" | ||
ErrUnsupportedResponseType ErrorType = "unsupported_response_type" | ||
ErrInvalidScope ErrorType = "invalid_scope" | ||
ErrServerError ErrorType = "server_error" | ||
ErrTemporarilyUnavailable ErrorType = "temporarily_unavailable" | ||
ErrAccessDenied ErrorType = "access_denied" | ||
|
||
ErrInvalidGrant ErrorType = "invalid_grant" | ||
|
||
ErrNeedValidation ErrorType = "need_validation" | ||
ErrNeedCaptcha ErrorType = "need_captcha" | ||
) | ||
|
||
// Error returns the message of a Error. | ||
func (e ErrorType) Error() string { | ||
return "oauth: error with type " + string(e) | ||
} | ||
|
||
// ErrorReason for oauth. | ||
type ErrorReason string | ||
|
||
// Error returns the message of a Error. | ||
func (e ErrorReason) Error() string { | ||
return "oauth: error with reason " + string(e) | ||
} | ||
|
||
// ErrorReason types. | ||
const ( | ||
ErrUserDenied ErrorReason = "user_denied" | ||
) | ||
|
||
// Error for oauth. | ||
type Error struct { | ||
Type ErrorType `json:"error"` | ||
Reason ErrorReason `json:"error_reason,omitempty"` | ||
Description string `json:"error_description,omitempty"` | ||
|
||
// For auth direct | ||
CaptchaSID string `json:"captcha_sid,omitempty"` | ||
CaptchaImg string `json:"captcha_img,omitempty"` | ||
RedirectURI string `json:"redirect_uri,omitempty"` | ||
ValidationType ValidationType `json:"validation_type,omitempty"` | ||
PhoneMask string `json:"phone_mask,omitempty"` | ||
} | ||
|
||
// Error returns the message of a Error. | ||
func (e Error) Error() string { | ||
if e.Description != "" { | ||
return "oauth: " + e.Description | ||
} | ||
|
||
return e.Type.Error() | ||
} | ||
|
||
// Is unwraps its first argument sequentially looking for an error that matches | ||
// the second. | ||
func (e Error) Is(target error) bool { | ||
var tError *Error | ||
if errors.As(target, &tError) { | ||
return e.Type == tError.Type && e.Description == tError.Description | ||
} | ||
|
||
var tErrorType ErrorType | ||
if errors.As(target, &tErrorType) { | ||
return e.Type == tErrorType | ||
} | ||
|
||
var tErrorReason ErrorReason | ||
if errors.As(target, &tErrorReason) { | ||
return e.Reason == tErrorReason | ||
} | ||
|
||
return false | ||
} |
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,76 @@ | ||
package oauth_test | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/SevereCloud/vksdk/v2/api/oauth" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type otherError string | ||
|
||
func (e otherError) Error() string { | ||
return string(e) | ||
} | ||
|
||
func TestErrorType(t *testing.T) { | ||
t.Parallel() | ||
|
||
err := oauth.ErrorType("aoa") | ||
assert.EqualError(t, err, "oauth: error with type aoa") | ||
} | ||
|
||
func TestErrorReason(t *testing.T) { | ||
t.Parallel() | ||
|
||
err := oauth.ErrorReason("aoa") | ||
assert.EqualError(t, err, "oauth: error with reason aoa") | ||
} | ||
|
||
func TestError_Error(t *testing.T) { | ||
t.Parallel() | ||
|
||
err := oauth.Error{ | ||
Type: oauth.ErrorType("aoa"), | ||
Description: "test message", | ||
} | ||
assert.EqualError(t, err, "oauth: test message") | ||
|
||
err = oauth.Error{ | ||
Type: oauth.ErrorType("aoa"), | ||
} | ||
assert.EqualError(t, err, "oauth: error with type aoa") | ||
} | ||
|
||
func TestError_Is(t *testing.T) { | ||
t.Parallel() | ||
|
||
f := func(err *oauth.Error, target error, want bool) { | ||
t.Helper() | ||
|
||
assert.Equal(t, want, errors.Is(err, target)) | ||
} | ||
|
||
f(&oauth.Error{Type: oauth.ErrorType("aoa")}, &oauth.Error{Type: oauth.ErrorType("aoa")}, true) | ||
f(&oauth.Error{Type: oauth.ErrAccessDenied}, oauth.ErrAccessDenied, true) | ||
f(&oauth.Error{Reason: oauth.ErrUserDenied}, oauth.ErrUserDenied, true) | ||
f(&oauth.Error{Type: oauth.ErrorType("aoa"), Description: "123"}, &oauth.Error{Type: oauth.ErrorType("aoa"), Description: "123"}, true) | ||
|
||
f(&oauth.Error{Type: oauth.ErrorType("aoa")}, &oauth.Error{Type: oauth.ErrorType("oao")}, false) | ||
f(&oauth.Error{Type: oauth.ErrorType("aoa")}, oauth.ErrorType("oao"), false) | ||
f(&oauth.Error{Reason: oauth.ErrorReason("aoa")}, oauth.ErrorReason("oao"), false) | ||
f(&oauth.Error{Type: oauth.ErrorType("aoa"), Description: "123"}, &oauth.Error{Type: oauth.ErrorType("aoa"), Description: "321"}, false) | ||
f(&oauth.Error{Type: oauth.ErrorType("aoa")}, otherError("test"), false) | ||
} | ||
|
||
func TestError_As(t *testing.T) { | ||
t.Parallel() | ||
|
||
var target *oauth.Error | ||
|
||
err := &oauth.Error{Type: oauth.ErrorType("aoa")} | ||
if !errors.As(err, &target) && target.Type == "aoa" { | ||
t.Error("As not working") | ||
} | ||
} |
Oops, something went wrong.