Last active
September 15, 2015 10:40
-
-
Save hearsid/2e84d04548015b2e2867 to your computer and use it in GitHub Desktop.
Nodejs , restler and jasmine-node used for testing restful api testing , since Javascript runs asynchronously the code has been written to test API's whose output can treat as input of next api call . You can create a config.js file which contains values shared between various files . Install all the requirements and use jasmine-nod phone_otp_ve…
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
var restler = require('restler'); | |
var fs = require('fs'); | |
var colors = require('colors'); | |
var util = require('util'); | |
var config = require('./config.js'); | |
var baseUrl = config.baseUrl; | |
describe("My project phone api's tests", function () { | |
var apiCall; | |
var _token; | |
// generate random phone every time | |
var phone = Math.round(Math.random() * 1000000000); | |
var otp, otpPassed; | |
console.log("Test started on " + baseUrl); | |
// get the csrf token | |
it("should get the token", function () { | |
restler.get(baseUrl + "/basic/token") | |
.on("success", function (data) { | |
success = 1; | |
console.log(colors.blue("Success of get the Token")); | |
console.log(data); | |
expect(data).toBeDefined(); | |
_token = data.token; | |
apiCall = 1; | |
}).on("complete", function (data) { | |
if (!success) | |
console.log(colors.red(util.inspect(data))); | |
}); | |
}); | |
// get otp to verify the phone | |
it("should get the otp", function () { | |
waitsFor(function () { | |
return apiCall == 1; | |
}); | |
runs(function () { | |
var success = 0; | |
restler.post(baseUrl + "phone/getphoneotp", { | |
data: { | |
_token: _token, | |
phoneNumber: phone | |
}, | |
headers: { | |
'X-Requested-With': 'XMLHttpRequest' | |
} | |
}).on("success", function (data) { | |
success = 1; | |
console.log(colors.blue("Success of get the OTP")); | |
console.log(data); | |
otp = data.otp; | |
otpPassed = otp ; | |
apiCall = 2 ; | |
}).on("complete", function (data) { | |
if (!success) | |
console.log(colors.red(util.inspect(data))); | |
}); | |
}); | |
}); | |
it("should verify the otp", function () { | |
var success = 0; | |
waitsFor(function() { | |
return apiCall == 2 ; | |
}); | |
runs(function() { | |
restler.post(baseUrl + "phone/verifyotp", { | |
data: { | |
_token: _token, | |
phoneNumber: phone, | |
otp: otpPassed | |
}, | |
headers: { | |
'X-Requested-With': 'XMLHttpRequest' | |
} | |
}).on("success", function (data) { | |
success = 1; | |
console.log(colors.blue("Success of verify otp")); | |
console.log(data); | |
apiCall = 3 ; | |
}).on("complete", function (data) { | |
if (!success) | |
console.log(colors.red(util.inspect(data))); | |
}); | |
}); | |
}); | |
// register the user | |
// SEND IMAGE FILE located in the same folder as this test file for registration | |
it("should register the user", function () { | |
var success = 0; | |
var userInfo = { | |
firstName: "John", | |
lastName: "Doe", | |
description: "I'm a getting used for testing .", | |
zipCode: "71151", | |
street: "Testing avenue", | |
city: "San Fransisco", | |
state: "California", | |
email: "[email protected]", | |
image: "" , | |
special_conditions : "This is the condition" , | |
dependents : JSON.stringify( [{ | |
name : "tina" , | |
age :22 | |
} ,{ | |
name : "tina" , | |
age :22 | |
}]) | |
}; | |
waitsFor(function() { | |
return apiCall == 3 ; | |
}) ; | |
runs(function() { | |
fs.stat('index.jpeg', function (err, stats) { | |
console.log(stats); | |
console.info("user register started"); | |
restler.post(baseUrl + 'user/register', { | |
multipart: true, | |
headers: { | |
'X-Requested-With': 'XMLHttpRequest' | |
}, | |
data: { | |
token: _token, | |
phone: phone, | |
first_name: userInfo.firstName, | |
last_name: userInfo.lastName, | |
description: userInfo.description, | |
zip_code: userInfo.zipCode, | |
street: userInfo.street, | |
city: userInfo.city, | |
state: userInfo.state, | |
email: userInfo.email, | |
device_os: "ios", | |
device_token: "asdfas7sad6f899998j", | |
special_conditions : userInfo.special_conditions , | |
dependents : userInfo.dependents , | |
"folder_id": "0", | |
"image": restler.file("index.jpeg", null, stats.size, null, "image/jpeg") , | |
} | |
}).on("success", function (data) { | |
success = 1; | |
console.log(colors.blue("Success of user register")); | |
console.log(data); | |
}).on("complete", function (data) { | |
if (!success) | |
console.log(colors.red(util.inspect(data))); | |
}); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment