diff --git a/README.md b/README.md index e838807..28f4c9f 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ func main() { req := appstore.IAPRequest{ ReceiptData: "your receipt data encoded by base64", } - resp, err := client.Verify(&req) + resp, err := client.Verify(req) } ``` diff --git a/appstore/validator_test.go b/appstore/validator_test.go index a2e5fde..9e32f12 100644 --- a/appstore/validator_test.go +++ b/appstore/validator_test.go @@ -2,6 +2,10 @@ package appstore import ( "errors" + "fmt" + "net/http" + "net/http/httptest" + "os" "reflect" "testing" "time" @@ -79,6 +83,21 @@ func TestNew(t *testing.T) { } } +func TestNewWithEnvironment(t *testing.T) { + expected := Client{ + URL: "https://buy.itunes.apple.com/verifyReceipt", + TimeOut: time.Second * 5, + } + + os.Setenv("IAP_ENVIRONMENT", "production") + actual := New() + os.Clearenv() + + if !reflect.DeepEqual(actual, expected) { + t.Errorf("got %v\nwant %v", actual, expected) + } +} + func TestNewWithConfig(t *testing.T) { config := Config{ IsProduction: true, @@ -96,6 +115,22 @@ func TestNewWithConfig(t *testing.T) { } } +func TestNewWithConfigTimeout(t *testing.T) { + config := Config{ + IsProduction: true, + } + + expected := Client{ + URL: "https://buy.itunes.apple.com/verifyReceipt", + TimeOut: time.Second * 5, + } + + actual := NewWithConfig(config) + if !reflect.DeepEqual(actual, expected) { + t.Errorf("got %v\nwant %v", actual, expected) + } +} + func TestVerify(t *testing.T) { client := New() @@ -110,3 +145,46 @@ func TestVerify(t *testing.T) { t.Errorf("got %v\nwant %v", actual, expected) } } + +func TestVerifyErrors(t *testing.T) { + server, client := testTools(199, "dummy response") + defer server.Close() + + req := IAPRequest{ + ReceiptData: "dummy data", + } + + expected := errors.New("An error occurred in IAP - code:199") + _, actual := client.Verify(req) + if !reflect.DeepEqual(actual, expected) { + t.Errorf("got %v\nwant %v", actual, expected) + } +} + +func TestVerifyTimeout(t *testing.T) { + // HTTP 100 is "continue" so it will time out + server, client := testTools(100, "dummy response") + defer server.Close() + + req := IAPRequest{ + ReceiptData: "dummy data", + } + + expected := errors.New("") + _, actual := client.Verify(req) + if !reflect.DeepEqual(reflect.TypeOf(actual), reflect.TypeOf(expected)) { + t.Errorf("got %v\nwant %v", actual, expected) + } +} + +func testTools(code int, body string) (*httptest.Server, *Client) { + + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(code) + w.Header().Set("Content-Type", "application/json") + fmt.Fprintln(w, body) + })) + + client := &Client{URL: server.URL, TimeOut: time.Second * 2} + return server, client +} diff --git a/playstore/validator_test.go b/playstore/validator_test.go index ce2525e..248944e 100644 --- a/playstore/validator_test.go +++ b/playstore/validator_test.go @@ -1,6 +1,7 @@ package playstore import ( + "errors" "os" "reflect" "testing" @@ -27,6 +28,18 @@ func TestInit(t *testing.T) { } } +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", @@ -50,6 +63,34 @@ func TestInitWithConfig(t *testing.T) { } } +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{ @@ -106,6 +147,16 @@ func TestVerifySubscription(t *testing.T) { // 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() @@ -126,3 +177,13 @@ func TestVerifyProduct(t *testing.T) { // 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) + } +}