190 lines
5.0 KiB
Go
190 lines
5.0 KiB
Go
package playstore
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
|
|
"code.google.com/p/goauth2/oauth"
|
|
)
|
|
|
|
func TestInit(t *testing.T) {
|
|
expected := &oauth.Config{
|
|
ClientId: "dummyId",
|
|
ClientSecret: "dummySecret",
|
|
Scope: "https://www.googleapis.com/auth/androidpublisher",
|
|
AuthURL: "https://accounts.google.com/o/oauth2/auth",
|
|
TokenURL: "https://accounts.google.com/o/oauth2/token",
|
|
}
|
|
os.Setenv("IAB_CLIENT_ID", "dummyId")
|
|
os.Setenv("IAB_CLIENT_SECRET", "dummySecret")
|
|
Init()
|
|
os.Clearenv()
|
|
actual := defaultConfig
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
t.Errorf("got %v\nwant %v", actual, expected)
|
|
}
|
|
}
|
|
|
|
func TestInitWithoutClientSecret(t *testing.T) {
|
|
expected := errors.New("Client Secret Key is required")
|
|
|
|
os.Setenv("IAB_CLIENT_ID", "dummyId")
|
|
actual := Init()
|
|
os.Clearenv()
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
t.Errorf("got %v\nwant %v", actual, expected)
|
|
}
|
|
}
|
|
|
|
func TestInitWithConfig(t *testing.T) {
|
|
expected := &oauth.Config{
|
|
ClientId: "dummyId",
|
|
ClientSecret: "dummySecret",
|
|
Scope: "https://www.googleapis.com/auth/androidpublisher",
|
|
AuthURL: "https://accounts.google.com/o/oauth2/auth",
|
|
TokenURL: "https://accounts.google.com/o/oauth2/token",
|
|
}
|
|
|
|
config := &oauth.Config{
|
|
ClientId: "dummyId",
|
|
ClientSecret: "dummySecret",
|
|
Scope: "https://www.googleapis.com/auth/androidpublisher",
|
|
AuthURL: "https://accounts.google.com/o/oauth2/auth",
|
|
TokenURL: "https://accounts.google.com/o/oauth2/token",
|
|
}
|
|
InitWithConfig(config)
|
|
actual := defaultConfig
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
t.Errorf("got %v\nwant %v", actual, expected)
|
|
}
|
|
}
|
|
|
|
func TestInitWithConfigErrors(t *testing.T) {
|
|
expected := errors.New("Client ID is required")
|
|
|
|
config := &oauth.Config{
|
|
Scope: "https://www.googleapis.com/auth/androidpublisher",
|
|
AuthURL: "https://accounts.google.com/o/oauth2/auth",
|
|
TokenURL: "https://accounts.google.com/o/oauth2/token",
|
|
}
|
|
actual := InitWithConfig(config)
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
t.Errorf("got %v\nwant %v", actual, expected)
|
|
}
|
|
|
|
expected = errors.New("Client Secret Key is required")
|
|
config = &oauth.Config{
|
|
ClientId: "dummyId",
|
|
Scope: "https://www.googleapis.com/auth/androidpublisher",
|
|
AuthURL: "https://accounts.google.com/o/oauth2/auth",
|
|
TokenURL: "https://accounts.google.com/o/oauth2/token",
|
|
}
|
|
actual = InitWithConfig(config)
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
t.Errorf("got %v\nwant %v", actual, expected)
|
|
}
|
|
}
|
|
|
|
func TestNew(t *testing.T) {
|
|
// Initialize config
|
|
_config := &oauth.Config{
|
|
ClientId: "dummyId",
|
|
ClientSecret: "dummySecret",
|
|
}
|
|
InitWithConfig(_config)
|
|
|
|
token := &oauth.Token{
|
|
AccessToken: "accessToken",
|
|
RefreshToken: "refreshToken",
|
|
Expiry: time.Unix(1234567890, 0).UTC(),
|
|
}
|
|
|
|
actual := New(token)
|
|
val, _ := actual.httpClient.Transport.(*oauth.Transport)
|
|
|
|
if !reflect.DeepEqual(val.Config, _config) {
|
|
t.Errorf("got %v\nwant %v", val.Config, _config)
|
|
}
|
|
|
|
if !reflect.DeepEqual(val.Token, token) {
|
|
t.Errorf("got %v\nwant %v", val.Token, token)
|
|
}
|
|
}
|
|
|
|
func TestSetTimeout(t *testing.T) {
|
|
timeout := time.Second * 3
|
|
SetTimeout(timeout)
|
|
|
|
if defaultTimeout != timeout {
|
|
t.Errorf("got %#v\nwant %#v", defaultTimeout, timeout)
|
|
}
|
|
}
|
|
|
|
func TestVerifySubscription(t *testing.T) {
|
|
Init()
|
|
|
|
// Exception scenario
|
|
token := &oauth.Token{
|
|
AccessToken: "accessToken",
|
|
RefreshToken: "refreshToken",
|
|
Expiry: time.Unix(1234567890, 0).UTC(),
|
|
}
|
|
|
|
client := New(token)
|
|
expected := "Get https://www.googleapis.com/androidpublisher/v2/applications/package/purchases/subscriptions/subscriptionID/tokens/purchaseToken?alt=json: OAuthError: updateToken: Unexpected HTTP status 400 Bad Request"
|
|
_, err := client.VerifySubscription("package", "subscriptionID", "purchaseToken")
|
|
|
|
if err.Error() != expected {
|
|
t.Errorf("got %v", err)
|
|
}
|
|
|
|
// TODO Nomal scenario
|
|
}
|
|
|
|
func TestVerifySubscriptionAndroidPublisherError(t *testing.T) {
|
|
client := Client{nil}
|
|
expected := errors.New("client is nil")
|
|
_, actual := client.VerifySubscription("package", "subscriptionID", "purchaseToken")
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
t.Errorf("got %v\nwant %v", actual, expected)
|
|
}
|
|
}
|
|
|
|
func TestVerifyProduct(t *testing.T) {
|
|
Init()
|
|
|
|
// Exception scenario
|
|
token := &oauth.Token{
|
|
AccessToken: "accessToken",
|
|
RefreshToken: "refreshToken",
|
|
Expiry: time.Unix(1234567890, 0).UTC(),
|
|
}
|
|
|
|
client := New(token)
|
|
expected := "Get https://www.googleapis.com/androidpublisher/v2/applications/package/purchases/products/productID/tokens/purchaseToken?alt=json: OAuthError: updateToken: Unexpected HTTP status 400 Bad Request"
|
|
_, err := client.VerifyProduct("package", "productID", "purchaseToken")
|
|
|
|
if err.Error() != expected {
|
|
t.Errorf("got %v", err)
|
|
}
|
|
|
|
// TODO Nomal scenario
|
|
}
|
|
|
|
func TestVerifyProductAndroidPublisherError(t *testing.T) {
|
|
client := Client{nil}
|
|
expected := errors.New("client is nil")
|
|
_, actual := client.VerifyProduct("package", "productID", "purchaseToken")
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
t.Errorf("got %v\nwant %v", actual, expected)
|
|
}
|
|
}
|