Skip to content

Commit

Permalink
Merge pull request #6 from ayeshLK/tests
Browse files Browse the repository at this point in the history
Add smoke test cases for Stripe connector
  • Loading branch information
ayeshLK authored Aug 2, 2024
2 parents af45f54 + 83f0454 commit 328fca4
Show file tree
Hide file tree
Showing 8 changed files with 1,818 additions and 0 deletions.
32 changes: 32 additions & 0 deletions ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@ dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "lang.error"
version = "0.0.0"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "lang.int"
Expand Down Expand Up @@ -205,6 +214,9 @@ dependencies = [
{org = "ballerina", name = "lang.value"},
{org = "ballerina", name = "observe"}
]
modules = [
{org = "ballerina", packageName = "log", moduleName = "log"}
]

[[package]]
org = "ballerina"
Expand Down Expand Up @@ -248,6 +260,9 @@ dependencies = [
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"}
]
modules = [
{org = "ballerina", packageName = "os", moduleName = "os"}
]

[[package]]
org = "ballerina"
Expand All @@ -258,6 +273,20 @@ dependencies = [
{org = "ballerina", name = "time"}
]

[[package]]
org = "ballerina"
name = "test"
version = "0.0.0"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.array"},
{org = "ballerina", name = "lang.error"}
]
modules = [
{org = "ballerina", packageName = "test", moduleName = "test"}
]

[[package]]
org = "ballerina"
name = "time"
Expand Down Expand Up @@ -296,7 +325,10 @@ version = "2.0.0"
dependencies = [
{org = "ballerina", name = "constraint"},
{org = "ballerina", name = "http"},
{org = "ballerina", name = "log"},
{org = "ballerina", name = "mime"},
{org = "ballerina", name = "os"},
{org = "ballerina", name = "test"},
{org = "ballerina", name = "url"},
{org = "ballerinai", name = "observe"}
]
Expand Down
99 changes: 99 additions & 0 deletions ballerina/tests/accounts_tests.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
//
// WSO2 LLC. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/test;

isolated string commonAccountId = "";

function setCommonAccountId(string accountId) {
lock {
commonAccountId = accountId;
}
}

function getCommonAccountId() returns string {
lock {
return commonAccountId;
}
}

@test:Config {
groups: ["live_tests", "mock_tests"]
}
function createAccountTest() returns error? {
accounts_body accountDetails = {
country: "US",
email: "jenny.rosen@example.com",
default_currency: "usd"
};
Account account = check stripe->/accounts.post(accountDetails);
test:assertTrue(account.country is string, "Could not find the country setting");
test:assertTrue(account.default_currency is string, "Could not find the currency setting");
setCommonAccountId(account.id);
}

@test:Config {
groups: ["live_tests", "mock_tests"],
dependsOn: [createAccountTest]
}
function getAccountsTest() returns error? {
AccountList accountsResponse = check stripe->/accounts;
Account[] accounts = accountsResponse.data;
test:assertTrue(accounts.length() > 0, "No accounts found");
}

@test:Config {
groups: ["live_tests", "mock_tests"],
dependsOn: [createAccountTest]
}
function getAccountTest() returns error? {
string accountId = getCommonAccountId();
Account account = check stripe->/accounts/[accountId];
test:assertEquals(account.id, accountId, "Invalid account received");
}

@test:Config {
groups: ["live_tests", "mock_tests"]
}
function deleteAccountTest() returns error? {
accounts_body accountDetails = {
country: "US",
email: "jenny.rosen.deleted@example.com",
default_currency: "usd"
};
Account account = check stripe->/accounts.post(accountDetails);

Deleted_account deletedAccount = check stripe->/accounts/[account.id].delete();
test:assertEquals(deletedAccount.id, account.id, "Invalid account deleted");
}

@test:Config {
groups: ["live_tests", "mock_tests"]
}
function rejectAccountTest() returns error? {
accounts_body accountDetails = {
country: "US",
email: "jenny.rosen.rejected@example.com",
default_currency: "usd"
};
Account account = check stripe->/accounts.post(accountDetails);

account_reject_body accountReject = {
reason: "fraud"
};
Account rejectedAccount = check stripe->/accounts/[account.id]/reject.post(accountReject);
test:assertEquals(rejectedAccount.id, account.id, "Invalid account rejected");
}
82 changes: 82 additions & 0 deletions ballerina/tests/charges_tests.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
//
// WSO2 LLC. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/test;

isolated string commonChargeId = "";

function setCommonChargeId(string chargeId) {
lock {
commonChargeId = chargeId;
}
}

function getCommonChargeId() returns string {
lock {
return commonChargeId;
}
}

@test:Config {
groups: ["live_tests", "mock_tests"]
}
function createChargeTest() returns error? {
charges_body chargeDetails = {
amount: 100,
currency: "usd",
'source: "tok_visa"
};
Charge charge = check stripe->/charges.post(chargeDetails);
test:assertEquals(charge.amount, 100, "Invalid charge amount");
setCommonChargeId(charge.id);
}

@test:Config {
groups: ["live_tests", "mock_tests"],
dependsOn: [createChargeTest]
}
function getChargeTest() returns error? {
string chargeId = getCommonChargeId();
Charge charge = check stripe->/charges/[chargeId];
test:assertEquals(charge.id, chargeId, "Invalid charge details found");
}

@test:Config {
groups: ["live_tests", "mock_tests"],
dependsOn: [createChargeTest]
}
function updateChargeTest() returns error? {
string chargeId = getCommonChargeId();
record {|string...;|} metadata = {
"shipping": "express"
};
charges_charge_body chargeUpdate = { metadata };
Charge charge = check stripe->/charges/[chargeId].post(chargeUpdate);
test:assertEquals(charge.metadata, metadata, "Invalid meta-data found");
}

@test:Config {
groups: ["live_tests", "mock_tests"],
dependsOn: [updateChargeTest]
}
function refundChargeTest() returns error? {
string chargeId = getCommonChargeId();
charge_refund_body chargeRefund = {
amount: 100
};
Charge charge = check stripe->/charges/[chargeId]/refund.post(chargeRefund);
test:assertTrue(charge.refunded, "Charge refund was unsuccessful");
}
72 changes: 72 additions & 0 deletions ballerina/tests/customers_tests.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
//
// WSO2 LLC. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/test;

isolated string commonCustomerId = "";

function setCommonCustomerId(string customerId) {
lock {
commonCustomerId = customerId;
}
}

function getCommonCustomerId() returns string {
lock {
return commonCustomerId;
}
}

@test:Config {
groups: ["live_tests", "mock_tests"]
}
function createCustomerTest() returns error? {
customers_body newCustomer = {
name: "John Doe",
email: "john.doe@sample.com",
address: {
city: "Colombo",
country: "Sri Lanka"
}
};
Customer customer = check stripe->/customers.post(newCustomer);
test:assertEquals(customer?.name, "John Doe", "Invalid customer name");
setCommonCustomerId(customer.id);
}

@test:Config {
groups: ["live_tests", "mock_tests"],
dependsOn: [createCustomerTest]
}
function updateCustomerTest() returns error? {
string customerId = getCommonCustomerId();
record {|string...;|} metadata = {
"businessType": "Stock trading"
};
customers_customer_body customerUpdate = { metadata };
Customer customer = check stripe->/customers/[customerId].post(customerUpdate);
test:assertEquals(customer.metadata, metadata, "Invalid meta-data found");
}

@test:Config {
groups: ["live_tests", "mock_tests"],
dependsOn: [updateCustomerTest]
}
function deleteCustomerTest() returns error? {
string customerId = getCommonCustomerId();
Deleted_customer deletedCustomer = check stripe->/customers/[customerId].delete();
test:assertTrue(deletedCustomer.deleted, "Customer deletion successful");
}
31 changes: 31 additions & 0 deletions ballerina/tests/init_tests.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
//
// WSO2 LLC. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/log;
import ballerina/os;

configurable boolean isLiveServer = os:getEnv("IS_LIVE_SERVER") == "true";
configurable string stripeSecret = isLiveServer ? os:getEnv("STRIPE_SECRET") : "test";

final Client stripe = check initClient();

function initClient() returns Client|error {
if isLiveServer {
log:printInfo("Running tests on actual server");
return new ({auth: {token: stripeSecret}});
}
return new ({auth: {token: stripeSecret}}, "http://localhost:9090/v1");
}
Loading

0 comments on commit 328fca4

Please sign in to comment.