usergrid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mru...@apache.org
Subject [1/2] usergrid-nodejs git commit: Initial commit of Usergrid Node.js SDK into its own repo
Date Fri, 02 Sep 2016 16:16:20 GMT
Repository: usergrid-nodejs
Updated Branches:
  refs/heads/master [created] 7b92555b5


http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/blob/7b92555b/readme.md
----------------------------------------------------------------------
diff --git a/readme.md b/readme.md
new file mode 100755
index 0000000..474d506
--- /dev/null
+++ b/readme.md
@@ -0,0 +1,720 @@
+# Usergrid Node.js SDK
+
+##Version
+
+Current Version: **0.10.8**
+
+See change log:
+
+<https://github.com/apache/usergrid-nodejs/blob/master/changelog.md>
+
+
+##Comments / Questions
+Please feel free to send comments or questions:
+
+	twitter: @rockerston
+	email: rockerston at apache.org
+
+Or just open github issues.  I truly want to know what you think, and will address all suggestions / comments / concerns.
+
+Thank you!
+
+Rod
+
+
+##Overview
+This Node.js module, which simplifies the process of making API calls to App Services from within Node.js, is available as an open-source project on github.  We welcome your contributions and suggestions. The repository is located here:
+
+<https://github.com/apache/usergrid-nodejs>
+
+
+##Client side Javascript
+Want to make calls to Usergrid client-side? No problem - just head over to the Usergrid Javascript SDK:
+
+<https://github.com/apache/usergrid-javascript>
+
+The syntax for this Node module and the Javascript SDK are almost exactly the same so you can easily transition between them.
+
+
+##Installing
+Use npm:
+
+	$ npm install usergrid
+
+
+##Getting started
+Include the module:
+
+	var usergrid = require('usergrid');
+
+Then create a new client:
+
+	var client = new usergrid.client({
+		orgName:'yourorgname',
+		appName:'sandbox',
+		logging: true, //optional - turn on logging, off by default
+	});
+
+The preceding example shows how to use the "Sandbox" testing app, which does not require any authentication.  The "Sandbox" comes with all new App Services accounts.
+
+If you are ready to use authentication, then create your client this way:
+
+	var client = new usergrid.client({
+		orgName:'yourorgname',
+		appName:'yourappname',
+		authType:usergrid.AUTH_CLIENT_ID,
+		clientId:'<your client id>',
+		clientSecret:'<your client secret>',
+		logging: false, //optional - turn on logging, off by default
+		buildCurl: false //optional - turn on curl commands, off by default
+	});
+
+The last two items are optional. The **logging** option will enable console.log output from the client.  The **buildCurl** option will cause cURL equivalent commands of all calls to the API to be displayed in the console.log output.
+
+You are now ready to use the usergrid handle to make calls against the API.
+
+
+##About the samples
+All of the samples provided in this readme file come from unit tests in the test.js which is located in the root of this project.
+
+
+To run the test file, first do the following:
+
+1. Change the org-name and app-name to point to your Usergrid organization account.  
+2. Change the client secret and client id
+
+Then run the code:
+
+	$ node test.js
+
+The samples in this file will show you the many ways you can use this module.
+
+
+##Entities and Collections
+Usergrid stores its data as "Entities" in "Collections".  Entities are essentially JSON objects and Collections are just like folders for storing these objects. You can learn more about Entities and Collections in the App Services docs:
+
+<http://usergrid.apache.org/docs/introduction/data-model.html>
+
+
+##Entities
+This module provides an easy way to make new entities. Here is a simple example that shows how to create a new object of type "dogs":
+
+	var options = {
+		type:'dogs',
+		name:'Dino'
+	}
+	client.createEntity(options, function (err, dog) {
+		if (err) {
+			//error - dog not created
+		} else {
+			//success -dog is created
+
+			//once the dog is created, you can set single properties:
+			dog.set('breed','Dinosaur');
+
+			//or a JSON object:
+			var data = {
+				master:'Fred',
+				state:'hungry'
+			}
+			//set is additive, so previously set properties are not overwritten
+			dog.set(data);
+
+			//finally, call save on the object to save it back to the database
+			dog.save(function(err){
+				if (err){
+					//error - dog not saved
+				} else {
+					//success - new dog is saved
+				}
+			});
+		}
+	});
+
+**note:** all calls to the API will be executed asynchronously, so it is important that you use a callback.
+
+
+You can also refresh the object from the database if needed (in case the data has been updated by a different client or device):
+
+	//call fetch to refresh the data from the server
+	dog.fetch(function(err){
+		if (err){
+			// error - dog not refreshed from database;
+		} else {
+			//dog has been refreshed from the database
+			//will only work if the UUID for the entity is in the dog object
+			//success - dog entity refreshed from database;
+		}
+	});
+
+To remove the entity from the database:
+
+	//the destroy method will delete the entity from the database
+	dog.destroy(function(err){
+		if (err){
+			//error - dog not removed from database
+		} else {
+			//success - dog removed from database (no real dogs were harmed!)
+			dog = null; //no real dogs were harmed!
+		}
+	});
+
+To set properties on the entity, use the set() method:
+
+	//once the dog is created, you can set single properties:
+	dog.set('breed','Dinosaur');
+
+	//or a JSON object:
+	var data = {
+		master:'Fred',
+		state:'hungry'
+	}
+	//set is additive, so previously set properties are not overwritten
+	dog.set(data);
+
+**Note:** These properties are now set locally, but make sure you call the .save() method on the entity to save them back to the database!
+
+To get a single property from the entity, use the get method:
+
+	var breed = dog.get('breed');
+
+or
+
+	var state = dog.get('state');
+
+or, to get a JSON object with all properties, don't pass a key
+
+	var props = dog.get();
+
+Based on the set statements above, our JSON object should look like this:
+
+	{
+		name:'Dino',
+		type:'dogs',
+		breed:'Dinosaur',
+		master:'Fred',
+		state:'hungry'
+	}
+
+**Wait!** But what if my entity already exists on the server?
+
+During a client.createEntity call, there are two ways that you can choose to handle this situation.  The question is, what should the client do if an entity with the same name, username, or uuid already exists on the server?
+
+  	1. Give you back an error.
+  	2. Give you back the pre-existing entity.
+
+If you want to get back an error when the entity already exists, then simply call the client.createEntity function as above. If there is a collision, you will get back a 400  However, if you want the existing entity to be returned, then set the getOnExist flag to true:
+
+	var options = {
+		type:'dogs',
+		name:'Dino',
+		getOnExist:true
+	}
+	client.createEntity(options, function (err, dog) {
+		if (err) {
+			//error - dog not created
+		} else {
+			//success -dog is created or returned, depending on if it already exists or not
+
+
+Alternatively, if you know that you only want to retrieve an existing entity, use the getEntity method:
+
+	var options = {
+		type:'users',
+		username:'marty'
+	}
+	client.getEntity(options, function(err, existingUser){
+		if (err){
+			//error - existing user not retrieved
+		} else {
+			//success - existing user was retrieved
+
+			var username = existingUser.get('username');
+		}
+	});
+
+
+##The Collection object
+The Collection object models Collections in the database.  Once you start programming your app, you will likely find that this is the most useful method of interacting with the database.  Creating a collection will automatically populate the object with entities from the collection. The following example shows how to create a Collection object, then how to use entities once the Collection has been populated with entities from the server:
+
+	//options object needs to have the type (which is the collection type)
+	var options = {
+		type:'dogs',
+		qs:{ql:'order by index'}
+	}
+
+	client.createCollection(options, function (err, dogs) {
+		if (err) {
+			//error - could not make collection
+		} else {
+
+			//success - new Collection worked
+
+			//we got the dogs, now display the Entities:
+			while(dogs.hasNextEntity()) {
+				//get a reference to the dog
+				dog = dogs.getNextEntity();
+				var name = dog.get('name');
+				notice('dog is called ' + name);
+			}
+
+			//success - looped through dogs
+
+		}
+	});
+
+
+You can also add a new entity of the same type to the collection:
+
+	//create a new dog and add it to the collection
+	var options = {
+		name:'extra-dog',
+		fur:'shedding'
+	}
+	//just pass the options to the addEntity method
+	//to the collection and it is saved automatically
+	dogs.addEntity(options, function(err, dog, data) {
+		if (err) {
+			//error - extra dog not saved or added to collection
+		} else {
+			//success - extra dog saved and added to collection
+		}
+	});
+
+
+##Collection iteration and paging
+The Collection object works in Pages of data.  This means that at any given time, the Collection object will have one page of data loaded.  You can iterate across all the entities in the current page of data by using the following pattern:
+
+	//we got the dogs, now display the Entities:
+	while(dogs.hasNextEntity()) {
+		//get a reference to the dog
+		dog = dogs.getNextEntity();
+		var name = dog.get('name');
+		notice('dog is called ' + name);
+	}
+
+To get the next page of data from the server, use the following pattern:
+
+	if (dogs.hasNextPage()) {
+		//there is a next page, so get it from the server
+		dogs.getNextPage(function(err){
+			if (err) {
+				//error - could not get next page of dogs
+			} else {
+				//success - got next page of dogs
+				//we got the dogs, now display the Entities:
+				while(dogs.hasNextEntity()) {
+					//get a reference to the dog
+					dog = dogs.getNextEntity();
+					var name = dog.get('name');
+					notice('dog is called ' + name);
+				}
+				//success - looped through dogs
+			}
+		});
+	}
+
+You can use the same pattern to get a previous page of data:
+
+	if (dogs.hasPreviousPage()) {
+		//there is a previous page, so get it from the server
+		dogs.getPreviousPage(function(err){
+			if(err) {
+				//error - could not get previous page of dogs
+			} else {
+				//success - got next page of dogs
+				//we got the dogs, now display the Entities:
+				while(dogs.hasNextEntity()) {
+					//get a reference to the dog
+					dog = dogs.getNextEntity();
+					var name = dog.get('name');
+					notice('dog is called ' + name);
+				}
+				//success - looped through dogs
+			}
+		});
+	}
+
+By default, the database will return 10 entities per page.  You can change that amount by setting a limit:
+
+
+	var options = {
+		type:'dogs',
+		qs:{limit:50} //limit statement set to 50
+	}
+
+	client.createCollection(options, function (err, dogs) {
+		if (err) {
+			//error - could not get all dogs
+		} else {
+			//success - got at most 50 dogs
+		}
+	}
+
+Several other convenience methods exist to make working with pages of data easier:
+
+* getFirstEntity - gets the first entity of a page
+* getLastEntity - gets the last entity of a page
+* resetEntityPointer - sets the internal pointer back to the first element of the page
+* getEntityByUUID - returns the entity if it is in the current page
+
+
+###Custom Queries
+A custom query allows you to tell the API that you want your results filtered or altered in some way.  To specify that the query results should be ordered by creation date, add the qs parameter to the options object:
+
+	var options = {
+		type:'dogs',
+		qs:{ql:'order by created DESC'}
+	};
+
+You may find that you need to change the query on an existing object.  Simply access the qs property directly:
+
+	dogs.qs = {ql:'order by created DESC'};
+
+
+If you also wanted to get more entities in the result set than the default 10, say 100, you can specify a query similar to the following (the limit can be a maximum of 999):
+
+	dogs.qs = {ql:'order by created DESC',limit:'100'};
+
+**Note**: there are many cases where expanding the result set is useful.  But be careful - the more results you get back in a single call, the longer it will take to transmit the data back to your app.
+
+Another common requirement is to limit the results to a specific query.  For example, to get all brown dogs, use the following syntax:
+
+	dogs.qs = {ql:"select * where color='brown'"};
+
+You can also limit the results returned such that only the fields you specify are returned:
+
+	dogs.qs = {'ql':"select name, age where color='brown'"};
+
+**Note:** in the two preceding examples that we put single quotes around 'brown', so it will be searched as a string.
+
+You can find more information on custom queries here:
+
+<http://usergrid.apache.org/docs/data-queries/querying-your-data.html>
+
+
+##Modeling users with the Entity object
+There is no specific User object in the module.  Instead, you simply need to use the Entity object, specifying a type of "users".  Here is an example:
+
+	//type is 'users', set additional paramaters as needed
+	var options = {
+		type:'users',
+		username:'marty',
+		password:'mysecurepassword',
+		name:'Marty McFly',
+		city:'Hill Valley'
+	}
+
+  	client.createEntity(options, function (err, marty) {
+		if (err){
+			//error - user not saved
+		} else {
+			//success - user saved
+		}
+	});
+
+
+If the user is modified, just call save on the user again:
+
+	//add properties cumulatively
+	marty.set('state', 'California');
+	marty.set("girlfriend","Jennifer");
+	marty.save(function(err){
+		if (err){
+			//error - user not updated
+		} else {
+			//success - user updated
+		}
+	});
+
+To refresh the user's information in the database:
+
+	marty.fetch(function(err){
+		if (err){
+			//error - not refreshed
+		} else {
+			//success - user refreshed
+		}
+	});
+
+If you no longer need the object, call the delete() method and the object will be deleted from database:
+
+	marty.destroy(function(err){
+		if (err){
+			//error - user not deleted from database
+		} else {
+			//success - user deleted from database
+			marty = null; //blow away the local object
+		}
+	});
+
+
+###Making connections
+Connections are a way to connect to entities with some verb.  This is called an entity relationship.  For example, if you have a user entity with username of marty, and a dog entity with a name of einstein, then using our RESTful API, you could make a call like this:
+
+	POST users/marty/likes/dogs/einstein
+
+This creates a one-way connection between marty and einstein, where marty "likes" einstein.
+
+Complete documentation on the entity relationships API can be found here:
+
+<http://usergrid.apache.org/docs/entity-connections/connecting-entities.html>
+
+The following code shows you how to create this connection, and then verify that the connection has been made:
+
+	marty.connect('likes', dog, function (err, data) {
+		if (err) {
+			// error - connection not created
+		} else {
+
+			//call succeeded, so pull the connections back down
+			marty.getConnections('likes', function (err, data) {
+				if (err) {
+						//error - could not get connections
+				} else {
+					//verify that connection exists
+					if (marty.likes.ralphy) {
+						//success - connection exists
+					} else {
+						//error - connection does not exist
+					}
+				}
+			});
+		}
+	});
+
+You can also remove connections, by using the disconnect method:
+
+	marty.disconnect('likes', dog, function (err, data) {
+		if (err) {
+			//error - connection not deleted
+		} else {
+
+			//call succeeded, so pull the connections back down
+			marty.getConnections('likes', function (err, data) {
+				if (err) {
+					//error - error getting connections
+				} else {
+					//verify that connection exists
+					if (marty.likes.einstein) {
+						//error - connection still exists
+					} else {
+						//success - connection deleted
+					}
+				}
+			});
+		}
+	});
+
+
+###To log a user in
+Up to this point, we have shown how you can use the client secret / client id combination to authenticate your calls against the API.  For a server-side Node.js app, this may be all you need.  However, if you do find that your app requires that you authenticate an individual user, you have several options.
+
+The first is to use client-side authentication with Ajax.  If you want to opt for this method, take a look at our Javascript SDK.  The syntax for usage is the same as this Node.js module, so it will be easy to pick up:
+
+<https://github.com/apache/usergrid-javascript>
+
+The other method is to log the user in server-side. When you log a user in, the API will return an OAuth token for you to use for calls to the API on the user's behalf.  Once that token is returned, you can either make a new client just for the user, or change the auth method on the existing client.  These methods are described below:
+
+
+	username = 'marty';
+	password = 'mysecurepassword';
+	client.login(username, password,
+		function (err) {
+			if (err) {
+				//error - could not log user in
+			} else {
+				//success - user has been logged in
+
+				//the login call will return an OAuth token, which is saved
+				//in the client object for later use.  Access it this way:
+				var token = client.token;
+
+				//then make a new client just for the app user, then use this
+				//client to make calls against the API
+				var appUserClient = new usergrid.client({
+					orgName:'yourorgname',
+					appName:'yourappname',
+					authType:usergrid.AUTH_APP_USER,
+					token:token
+				});
+
+				//alternitavely, you can change the authtype of the client:
+				client.authType = usergrid.AUTH_APP_USER;
+
+				//Then make calls against the API.  For example, you can
+				//get the user entity this way:
+				client.getLoggedInUser(function(err, data, user) {
+					if(err) {
+						//error - could not get logged in user
+					} else {
+						//success - got logged in user
+
+						//you can then get info from the user entity object:
+						var username = user.get('username');
+
+						//to log the user out, call the logout() method
+						appUserClient.logout();
+						client.logout();
+
+						//verify the logout worked
+						if (client.isLoggedIn()) {
+							//error - logout failed
+						} else {
+							//success - user has been logged out
+						}
+
+						//since we don't need to App User level calls anymore,
+						//set the authtype back to client:
+						client.authType = usergrid.AUTH_CLIENT_ID;
+
+						runner(step, marty);
+					}
+				});
+
+			}
+		}
+	);
+
+
+To recap, once a user has been logged in, and an OAuth token has been acquired, use one of the two methods to make calls to the API:
+
+1. Use the same client object and change auth types before each call
+
+2. Grab the token and make a new client object specifically for user calls.
+
+Either method will work.
+
+
+###To log a user out
+To log the user out, call:
+
+	client.logout();
+
+Or, if you made a new client object specifically for the app user:
+
+	appUserClient.logout();
+
+This destroys the token and user object in the client object, effectively logging the user out.
+
+##Groups
+This module provides an easy way to make new groups. They follow the same syntax as Entities
+
+##Making generic calls
+If you find that you need to make calls to the API that fall outside of the scope of the Entity and Collection objects, you can use the following format to make any REST calls against the API:
+
+	client.request(options, callback);
+
+This format allows you to make almost any call against the App Services (Usergrid) API. For example, to get a list of users:
+
+	var options = {
+		method:'GET',
+		endpoint:'users'
+	};
+	client.request(options, function (err, data) {
+		if (err) {
+			//error - GET failed
+		} else {
+			//data will contain raw results from API call
+			//success - GET worked
+		}
+	});
+
+Or, to create a new user:
+
+	var options = {
+		method:'POST',
+		endpoint:'users',
+		body:{ username:'fred', password:'secret' }
+	};
+	client.request(options, function (err, data) {
+		if (err) {
+			//error - POST failed
+		} else {
+			//data will contain raw results from API call
+			//success - POST worked
+		}
+	});
+
+Or, to update the new user:
+
+	var options = {
+		method:'PUT',
+		endpoint:'users/fred',
+		body:{ newkey:'newvalue' }
+	};
+	client.request(options, function (err, data) {
+		if (err) {
+			//error - PUT failed
+		} else {
+			//data will contain raw results from API call
+			//success - PUT worked
+		}
+	});
+
+Or to delete the new user:
+
+	var options = {
+		method:'DELETE',
+		endpoint:'users/fred'
+	};
+	client.request(options, function (err, data) {
+		if (err) {
+			//error - DELETE failed
+		} else {
+			//data will contain raw results from API call
+			//success - DELETE worked
+		}
+	});
+
+The Options Object for the client.request fuction:
+
+* `method` - http method (GET, POST, PUT, or DELETE), defaults to GET
+* `qs` - object containing querystring values to be appended to the uri
+* `body` - object containing entity body for POST and PUT requests
+* `endpoint` - API endpoint, for example "users/fred"
+* `mQuery` - boolean, set to true if running management query, defaults to false
+* `buildCurl` - boolean, set to true if you want to see equivalent curl commands in console.log, defaults to false
+
+You can make any call to the API using the format above.  However, in practice using the higher level Entity and Collection objects will make life easier as they take care of much of the heavy lifting.
+
+
+###cURL
+[cURL](http://curl.haxx.se/) is an excellent way to make calls directly against the API. As mentioned in the **Getting started** section of this guide, one of the parameters you can add to the new client options object is **buildCurl**:
+
+	var client = new Usergrid.Client({
+		orgName:'yourorgname',
+		appName:'sandbox',
+		logging: true, //optional - turn on logging, off by default
+		buildCurl: true //optional - turn on curl commands, off by default
+	});
+
+If you set this parameter to true, the SDK will build equivalent curl commands and send them to the console.log window.
+
+More information on cURL can be found here:
+
+<http://curl.haxx.se/>
+
+## Contributing
+We welcome your enhancements!
+
+Like [Usergrid](https://github.com/apache/usergrid-nodejs), the Usergrid Javascript SDK is open source and licensed under the Apache License, Version 2.0.
+
+1. Fork it
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Added some feature'`)
+4. Push your changes to the upstream branch (`git push origin my-new-feature`)
+5. Create new Pull Request (make sure you describe what you did and why your mod is needed)
+
+
+Licensed 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.
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/blob/7b92555b/test.js
----------------------------------------------------------------------
diff --git a/test.js b/test.js
new file mode 100755
index 0000000..354f8cb
--- /dev/null
+++ b/test.js
@@ -0,0 +1,1077 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF 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.
+//
+
+/**
+* Test suite for all the examples in the readme
+*
+* NOTE: No, this test suite doesn't use the traditional format for
+* a test suite.  This is because the goal is to require as little
+* alteration as possible during the copy / paste operation from this test
+* suite to the readme file.
+*
+* @author rod simpson (rod@apigee.com)
+*/
+
+
+var usergrid = require('./lib/usergrid');
+
+var logSuccess = true;
+var successCount = 0;
+var logError = true;
+var errorCount = 0;
+var logNotice = true;
+var _unique = new Date().getTime();
+var _username = 'marty'+_unique;
+var _email = 'marty'+_unique+'@timetravel.com';
+var _password = 'password2';
+var _newpassword = 'password3';
+
+var client = new usergrid.client({
+	orgName:'yourorgname',
+	appName:'sandbox',
+	logging: true, //optional - turn on logging, off by default
+	buildCurl: true //optional - turn on curl commands, off by default
+});
+
+
+//call the runner function to start the process
+client.logout();
+runner(0);
+
+function runner(step, arg, arg2){
+	step++;
+	switch(step)
+	{
+		case 1:
+			notice('-----running step '+step+': DELETE user from DB to prep test');
+			clearUser(step);
+			break;
+		case 2:
+			notice('-----running step '+step+': GET test');
+			testGET(step);
+			break;
+		case 3:
+			notice('-----running step '+step+': POST test');
+			testPOST(step);
+			break;
+		case 4:
+			notice('-----running step '+step+': PUT test');
+			testPUT(step);
+			break;
+		case 5:
+			notice('-----running step '+step+': DELETE test');
+			testDELETE(step);
+			break;
+		case 6:
+			notice('-----running step '+step+': prepare database - remove all dogs (no real dogs harmed here!!)');
+			cleanupAllDogs(step);
+			break;
+		case 7:
+			notice('-----running step '+step+': make a new dog');
+			makeNewDog(step);
+			break;
+		case 8:
+			notice('-----running step '+step+': update our dog');
+			updateDog(step, arg);
+			break;
+		case 9:
+			notice('-----running step '+step+': refresh our dog');
+			refreshDog(step, arg);
+			break;
+		case 10:
+			notice('-----running step '+step+': remove our dog from database (no real dogs harmed here!!)');
+			removeDogFromDatabase(step, arg);
+			break;
+		case 11:
+			notice('-----running step '+step+': make lots of dogs!');
+			makeSampleData(step, arg);
+			break;
+		case 12:
+			notice('-----running step '+step+': make a dogs collection and show each dog');
+			testDogsCollection(step);
+			break;
+		case 13:
+			notice('-----running step '+step+': get the next page of the dogs collection and show each dog');
+			getNextDogsPage(step, arg);
+			break;
+		case 14:
+			notice('-----running step '+step+': get the previous page of the dogs collection and show each dog');
+			getPreviousDogsPage(step, arg);
+			break;
+		case 15:
+			notice('-----running step '+step+': remove all dogs from the database (no real dogs harmed here!!)');
+			cleanupAllDogs(step);
+			break;
+		case 16:
+			notice('-----running step '+step+': prepare database (remove existing user if present)');
+			prepareDatabaseForNewUser(step);
+			break;
+		case 17:
+			notice('-----running step '+step+': create a new user');
+			createUser(step);
+			break;
+		case 18:
+			notice('-----running step '+step+': update the user');
+			updateUser(step, arg);
+			break;
+		case 19:
+			notice('-----running step '+step+': get the existing user');
+			getExistingUser(step, arg);
+			break;
+		case 20:
+			notice('-----running step '+step+': refresh the user from the database');
+			refreshUser(step, arg);
+			break;
+		case 21:
+			notice('-----running step '+step+': log user in');
+			loginUser(step, arg);
+			break;
+		case 22:
+			notice('-----running step '+step+': change users password');
+			changeUsersPassword(step, arg);
+			break;
+		case 23:
+			notice('-----running step '+step+': log user out');
+			logoutUser(step, arg);
+			break;
+		case 24:
+			notice('-----running step '+step+': relogin user');
+			reloginUser(step, arg);
+			break;
+		case 25:
+			notice('-----running step '+step+': logged in user creates dog');
+			createDog(step, arg);
+			break;
+		case 26:
+			notice('-----running step '+step+': logged in user likes dog');
+			userLikesDog(step, arg, arg2);
+			break;
+		case 27:
+			notice('-----running step '+step+': logged in user removes likes connection to dog');
+			removeUserLikesDog(step, arg, arg2);
+			break;
+		case 28:
+			notice('-----running step '+step+': user removes dog');
+			removeDog(step, arg, arg2);
+			break;
+		case 29:
+			notice('-----running step '+step+': log the user out');
+			logoutUser(step, arg);
+			break;
+		case 30:
+			notice('-----running step '+step+': remove the user from the database');
+			destroyUser(step, arg);
+			break;
+		case 31:
+			notice('-----running step '+step+': try to create existing entity');
+			createExistingEntity(step, arg);
+			break;
+		case 32:
+			notice('-----running step '+step+': try to create new entity with no name');
+			createNewEntityNoName(step, arg);
+			break;
+		case 33:
+			notice('-----running step '+step+': clean up users');
+      cleanUpUsers(step, arg);
+			break;
+		case 34:
+			notice('-----running step '+step+': clean up dogs');
+      cleanUpDogs(step, arg);
+			break;
+		case 35:
+			notice('-----running step '+step+': create counter');
+      counterCreate(step, arg);
+			break;
+		case 36:
+			notice('-----running step '+step+': reset counter');
+      counterReset(step, arg);
+			break;
+		case 37:
+			notice('-----running step '+step+': increment counter');
+      counterIncrement(step, arg);
+			break;
+		case 38:
+			notice('-----running step '+step+': decrement counter');
+      counterDecrement(step, arg);
+			break;
+		case 34:
+			notice('-----running step '+step+': fetch counter data');
+      counterFetch(step, arg);
+			break;
+		default:
+			notice('-----test complete!-----');
+			notice('Success count= ' + successCount);
+			notice('Error count= ' + errorCount);
+			notice('-----thank you for playing!-----');
+	}
+}
+
+//logging functions
+function success(message){
+	successCount++;
+	if (logSuccess) {
+		console.log('SUCCESS: ' + message);
+	}
+}
+
+function error(message){
+	errorCount++
+	if (logError) {
+		console.log('ERROR: ' + message);
+	}
+}
+
+function notice(message){
+	if (logNotice) {
+		console.log('NOTICE: ' + message);
+	}
+}
+
+//tests
+function clearUser(step) {
+  var options = {
+    method:'DELETE',
+    endpoint:'users/fred'
+  };
+  client.request(options, function (err, data) {
+    //data will contain raw results from API call
+    success('User cleared from DB');
+    runner(step);
+  });
+}
+
+function testGET(step) {
+	var options = {
+		method:'GET',
+		endpoint:'users'
+	};
+	client.request(options, function (err, data) {
+		if (err) {
+			error('GET failed');
+		} else {
+			//data will contain raw results from API call
+			success('GET worked');
+			runner(step);
+		}
+	});
+}
+
+function testPOST(step) {
+	var options = {
+		method:'POST',
+		endpoint:'users',
+		body:{ username:'fred', password:'secret' }
+	};
+	client.request(options, function (err, data) {
+		if (err) {
+			error('POST failed');
+		} else {
+			//data will contain raw results from API call
+			success('POST worked');
+			runner(step);
+		}
+	});
+}
+
+function testPUT(step) {
+	var options = {
+		method:'PUT',
+		endpoint:'users/fred',
+		body:{ newkey:'newvalue' }
+	};
+	client.request(options, function (err, data) {
+		if (err) {
+			error('PUT failed');
+		} else {
+			//data will contain raw results from API call
+			success('PUT worked');
+			runner(step);
+		}
+	});
+}
+
+function testDELETE(step) {
+	var options = {
+		method:'DELETE',
+		endpoint:'users/fred'
+	};
+	client.request(options, function (err, data) {
+		if (err) {
+			error('DELETE failed');
+		} else {
+			//data will contain raw results from API call
+			success('DELETE worked');
+			runner(step);
+		}
+	});
+}
+
+function makeNewDog(step) {
+
+	var options = {
+		type:'dogs',
+		name:'Ralph'+_unique
+	}
+
+	client.createEntity(options, function (err, dog) {
+		if (err) {
+			error('dog not created');
+		} else {
+			success('dog is created');
+
+			//once the dog is created, you can set single properties:
+			dog.set('breed','Dinosaur');
+
+			//the set function can also take a JSON object:
+			var data = {
+				master:'Fred',
+				state:'hungry'
+			}
+			//set is additive, so previously set properties are not overwritten
+			dog.set(data);
+
+			//finally, call save on the object to save it back to the database
+			dog.save(function(err){
+				if (err){
+					error('dog not saved');
+				} else {
+					success('new dog is saved');
+					runner(step, dog);
+				}
+			});
+		}
+	});
+
+}
+
+function updateDog(step, dog) {
+
+	//change a property in the object
+	dog.set("state", "fed");
+	//and save back to the database
+	dog.save(function(err){
+		if (err){
+			error('dog not saved');
+		} else {
+			success('dog is saved');
+			runner(step, dog);
+		}
+	});
+
+}
+
+function refreshDog(step, dog){
+
+	//call fetch to refresh the data from the server
+	dog.fetch(function(err){
+		if (err){
+			error('dog not refreshed from database');
+		} else {
+			//dog has been refreshed from the database
+			//will only work if the UUID for the entity is in the dog object
+			success('dog entity refreshed from database');
+			runner(step, dog);
+		}
+	});
+
+}
+
+function removeDogFromDatabase(step, dog){
+
+	//the destroy method will delete the entity from the database
+	dog.destroy(function(err){
+		if (err){
+			error('dog not removed from database');
+		} else {
+			success('dog removed from database'); // no real dogs were harmed!
+			dog = null; //no real dogs were harmed!
+			runner(step, 1);
+		}
+	});
+
+}
+
+function makeSampleData(step, i) {
+	notice('making dog '+i);
+
+	var options = {
+		type:'dogs',
+		name:'dog'+_unique+i,
+		index:i
+	}
+
+	client.createEntity(options, function (err, dog) {
+		if (err) {
+			error('dog ' + i + ' not created');
+		} else {
+			if (i >= 30) {
+				//data made, ready to go
+				success('all dogs made');
+				runner(step);
+			} else {
+				success('dog ' + i + ' made');
+				//keep making dogs
+				makeSampleData(step, ++i);
+			}
+		}
+	});
+}
+
+function testDogsCollection(step) {
+
+	var options = {
+		type:'dogs',
+		qs:{ql:'order by index'}
+	}
+
+	client.createCollection(options, function (err, dogs) {
+		if (err) {
+			error('could not make collection');
+		} else {
+
+			success('new Collection created');
+
+			//we got the dogs, now display the Entities:
+			while(dogs.hasNextEntity()) {
+				//get a reference to the dog
+				dog = dogs.getNextEntity();
+				var name = dog.get('name');
+				notice('dog is called ' + name);
+			}
+
+			success('looped through dogs');
+
+			//create a new dog and add it to the collection
+			var options = {
+				name:'extra-dog',
+				fur:'shedding'
+			}
+			//just pass the options to the addEntity method
+			//to the collection and it is saved automatically
+			dogs.addEntity(options, function(err, dog, data) {
+				if (err) {
+					error('extra dog not saved or added to collection');
+				} else {
+					success('extra dog saved and added to collection');
+					runner(step, dogs);
+				}
+			});
+		}
+	});
+
+}
+
+function getNextDogsPage(step, dogs) {
+
+	if (dogs.hasNextPage()) {
+		//there is a next page, so get it from the server
+		dogs.getNextPage(function(err){
+			if (err) {
+				error('could not get next page of dogs');
+			} else {
+				success('got next page of dogs');
+				//we got the next page of data, so do something with it:
+				var i = 11;
+				while(dogs.hasNextEntity()) {
+					//get a reference to the dog
+					var dog = dogs.getNextEntity();
+					var index = dog.get('index');
+					if(i !== index) {
+						error('wrong dog loaded: wanted' + i + ', got ' + index);
+					}
+					notice('got dog ' + i);
+					i++
+				}
+				success('looped through dogs')
+				runner(step, dogs);
+			}
+		});
+	} else {
+		getPreviousDogsPage(dogs);
+	}
+
+}
+
+function getPreviousDogsPage(step, dogs) {
+
+	if (dogs.hasPreviousPage()) {
+		//there is a previous page, so get it from the server
+		dogs.getPreviousPage(function(err){
+			if(err) {
+				error('could not get previous page of dogs');
+			} else {
+				success('got next page of dogs');
+				//we got the previous page of data, so do something with it:
+				var i = 1;
+				while(dogs.hasNextEntity()) {
+					//get a reference to the dog
+					var dog = dogs.getNextEntity();
+					var index = dog.get('index');
+					if(i !== index) {
+						error('wrong dog loaded: wanted' + i + ', got ' + index);
+					}
+					notice('got dog ' + i);
+					i++
+				}
+				success('looped through dogs');
+				runner(step);
+			}
+		});
+	} else {
+		getAllDogs();
+	}
+}
+
+function cleanupAllDogs(step){
+
+	var options = {
+		type:'dogs',
+		qs:{limit:50} //limit statement set to 50
+	}
+
+	client.createCollection(options, function (err, dogs) {
+		if (err) {
+			error('could not get all dogs');
+		} else {
+			success('got at most 50 dogs');
+			//we got 50 dogs, now display the Entities:
+			while(dogs.hasNextEntity()) {
+				//get a reference to the dog
+				var dog = dogs.getNextEntity();
+				var name = dog.get('name');
+				notice('dog is called ' + name);
+			}
+			dogs.resetEntityPointer();
+			//do doggy cleanup
+			while(dogs.hasNextEntity()) {
+				//get a reference to the dog
+				var dog = dogs.getNextEntity();
+				var dogname = dog.get('name');
+				notice('removing dog ' + dogname + ' from database');
+				dog.destroy(function(err, data) {
+					if (err) {
+						error('dog not removed');
+					} else {
+						success('dog removed');
+					}
+				});
+			}
+
+			//no need to wait around for dogs to be removed, so go on to next test
+			runner(step);
+		}
+	});
+}
+
+
+function prepareDatabaseForNewUser(step) {
+	var options = {
+		method:'DELETE',
+		endpoint:'users/',
+    qs:{ql:"select * where username ='marty*'"}
+	};
+	client.request(options, function (err, data) {
+		if (err) {
+			notice('database ready - no user to delete');
+		runner(step);
+		} else {
+			//data will contain raw results from API call
+			success('database ready - user deleted worked');
+			runner(step);
+		}
+	});
+}
+
+function createUser(step) {
+	client.signup(_username, _password, _email, 'Marty McFly',
+		function (err, marty) {
+			if (err){
+				error('user not created');
+				runner(step, marty);
+			} else {
+				success('user created');
+				runner(step, marty);
+			}
+		}
+	);
+}
+
+function updateUser(step, marty) {
+
+	//add properties cumulatively
+	marty.set('state', 'California');
+	marty.set("girlfriend","Jennifer");
+	marty.save(function(err){
+		if (err){
+			error('user not updated');
+		} else {
+			success('user updated');
+			runner(step, marty);
+		}
+	});
+
+}
+
+function getExistingUser(step, marty) {
+
+	var options = {
+		type:'users',
+		username:_username
+	}
+	client.getEntity(options, function(err, existingUser){
+		if (err){
+			error('existing user not retrieved');
+		} else {
+			success('existing user was retrieved');
+
+			var username = existingUser.get('username');
+			if (username === _username){
+				success('got existing user username');
+			} else {
+				error('could not get existing user username');
+			}
+			runner(step, marty);
+		}
+	});
+
+}
+
+
+function refreshUser(step, marty) {
+
+	marty.fetch(function(err){
+		if (err){
+			error('not refreshed');
+		} else {
+			success('user refreshed');
+			runner(step, marty);
+		}
+	});
+
+}
+
+function loginUser(step, marty) {
+	username = _username;
+	password = _password;
+	client.login(username, password,
+		function (err) {
+			if (err) {
+				error('could not log user in');
+			} else {
+				success('user has been logged in');
+
+				//the login call will return an OAuth token, which is saved
+				//in the client object for later use.  Access it this way:
+				var token = client.token;
+
+				//then make a new client just for the app user, then use this
+				//client to make calls against the API
+				var appUserClient = new usergrid.client({
+					orgName:'yourorgname',
+					appName:'yourappname',
+					authType:usergrid.AUTH_APP_USER,
+					token:token
+				});
+
+				//alternitavely, you can change the authtype of the client:
+				client.authType = usergrid.AUTH_APP_USER;
+
+				//Then make calls against the API.  For example, you can
+				//get the user entity this way:
+				client.getLoggedInUser(function(err, data, user) {
+					if(err) {
+						error('could not get logged in user');
+					} else {
+						success('got logged in user');
+
+						//you can then get info from the user entity object:
+						var username = user.get('username');
+						notice('logged in user was: ' + username);
+
+						runner(step, user);
+					}
+				});
+
+			}
+		}
+	);
+}
+
+function changeUsersPassword(step, marty) {
+
+	marty.set('oldpassword', _password);
+	marty.set('newpassword', _newpassword);
+	marty.save(function(err){
+		if (err){
+			error('user password not updated');
+		} else {
+			success('user password updated');
+			runner(step, marty);
+		}
+	});
+
+}
+
+function logoutUser(step, marty) {
+
+	//to log the user out, call the logout() method
+	client.logout();
+
+	//verify the logout worked
+	if (client.isLoggedIn()) {
+		error('logout failed');
+	} else {
+		success('user has been logged out');
+	}
+
+	runner(step, marty);
+}
+
+function reloginUser(step, marty) {
+
+	username = _username
+	password = _newpassword;
+	client.login(username, password,
+		function (err) {
+		if (err) {
+			error('could not relog user in');
+		} else {
+			success('user has been re-logged in');
+			runner(step, marty);
+		}
+		}
+	);
+}
+
+
+
+//TODO: currently, this code assumes permissions have been set to support user actions.  need to add code to show how to add new role and permission programatically
+//
+//first create a new permission on the default role:
+//POST "https://api.usergrid.com/yourorgname/yourappname/roles/default/permissions" -d '{"permission":"get,post,put,delete:/dogs/**"}'
+//then after user actions, delete the permission on the default role:
+//DELETE "https://api.usergrid.com/yourorgname/yourappname/roles/default/permissions?permission=get%2Cpost%2Cput%2Cdelete%3A%2Fdogs%2F**"
+
+
+function createDog(step, marty) {
+  //see if marty can create a new dog now that he is logged in
+
+	var options = {
+		type:'dogs',
+		name:'einstein',
+		breed:'mutt'
+	}
+
+	client.createEntity(options, function (err, dog) {
+		if (err) {
+			error('POST new dog by logged in user failed');
+		} else {
+			success('POST new dog by logged in user succeeded');
+			runner(step, marty, dog);
+		}
+	});
+
+}
+
+function userLikesDog(step, marty, dog) {
+
+	marty.connect('likes', dog, function (err, data) {
+		if (err) {
+			error('connection not created');
+			runner(step, marty);
+		} else {
+
+			//call succeeded, so pull the connections back down
+			marty.getConnections('likes', function (err, data) {
+				if (err) {
+						error('could not get connections');
+				} else {
+					//verify that connection exists
+					if (marty.likes.einstein) {
+						success('connection exists');
+					} else {
+						error('connection does not exist');
+					}
+
+					runner(step, marty, dog);
+				}
+			});
+		}
+	});
+
+}
+
+function removeUserLikesDog(step, marty, dog) {
+
+	marty.disconnect('likes', dog, function (err, data) {
+		if (err) {
+			error('connection not deleted');
+			runner(step, marty);
+		} else {
+
+			//call succeeded, so pull the connections back down
+			marty.getConnections('likes', function (err, data) {
+				if (err) {
+					error('error getting connections');
+				} else {
+					//verify that connection exists
+					if (marty.likes.einstein) {
+						error('connection still exists');
+					} else {
+						success('connection deleted');
+					}
+
+					runner(step, marty, dog);
+				}
+			});
+		}
+	});
+
+}
+
+function removeDog(step, marty, dog) {
+
+	//now delete the dog from the database
+	dog.destroy(function(err, data) {
+		if (err) {
+			error('dog not removed');
+		} else {
+			success('dog removed');
+		}
+	});
+	runner(step, marty);
+}
+
+function destroyUser(step, marty) {
+
+	marty.destroy(function(err){
+		if (err){
+			error('user not deleted from database');
+		} else {
+			success('user deleted from database');
+			marty = null; //blow away the local object
+			runner(step);
+		}
+	});
+
+}
+
+function createExistingEntity(step, marty) {
+
+	var options = {
+		type:'dogs',
+		name:'einstein'
+	}
+
+	client.createEntity(options, function (err, dog) {
+		if (err) {
+			error('Create new entity to use for existing entity failed');
+		} else {
+			success('Create new entity to use for existing entity succeeded');
+
+			var uuid = dog.get('uuid');
+			//now create new entity, but use same entity name of einstein.  This means that
+			//the original einstein entity now exists.  Thus, the new einstein entity should
+			//be the same as the original + any data differences from the options var:
+
+			options = {
+				type:'dogs',
+				name:'einstein',
+				breed:'mutt'
+			}
+			client.createEntity(options, function (err, newdog) {
+				if (err) {
+					error('Create new entity to use for existing entity failed');
+				} else {
+					success('Create new entity to use for existing entity succeeded');
+
+					var newuuid = newdog.get('uuid');
+					if (newuuid === uuid) {
+						success('UUIDs of new and old entities match');
+					} else {
+						error('UUIDs of new and old entities do not match');
+					}
+
+					var breed = newdog.get('breed');
+					if (breed === 'mutt') {
+						success('attribute sucesfully set on new entity');
+					} else {
+						error('attribute not sucesfully set on new entity');
+					}
+
+					newdog.destroy(function(err){
+						if (err){
+							error('existing entity not deleted from database');
+						} else {
+							success('existing entity deleted from database');
+							dog = null; //blow away the local object
+							newdog = null; //blow away the local object
+							runner(step);
+						}
+					});
+
+				}
+			});
+		}
+	});
+
+}
+
+function createNewEntityNoName(step, marty) {
+
+	var options = {
+   type:"something",
+   othervalue:"something else"
+	}
+
+	client.createEntity(options, function (err, entity) {
+		if (err) {
+			error('Create new entity with no name failed');
+		} else {
+			success('Create new entity with no name succeeded');
+
+      entity.destroy();
+      runner(step);
+		}
+	});
+
+}
+
+function cleanUpUsers(step){
+
+  var options = {
+    type:'users',
+    qs:{limit:50} //limit statement set to 50
+  }
+
+  client.createCollection(options, function (err, users) {
+    if (err) {
+      error('could not get all users');
+    } else {
+      success('got users');
+      //do doggy cleanup
+      while(users.hasNextEntity()) {
+        //get a reference to the dog
+        var user = users.getNextEntity();
+        var username = user.get('name');
+        notice('removing dog ' + username + ' from database');
+        user.destroy(function(err, data) {
+          if (err) {
+            error('user not removed');
+          } else {
+            success('user removed');
+          }
+        });
+      }
+
+      runner(step);
+    }
+  });
+
+}
+
+function cleanUpDogs(step){
+
+    var options = {
+      type:'dogs',
+      qs:{limit:50} //limit statement set to 50
+    }
+
+    client.createCollection(options, function (err, dogs) {
+      if (err) {
+        error('could not get all dogs');
+      } else {
+        success('got at most 50 dogs');
+        //we got 50 dogs, now display the Entities:
+        while(dogs.hasNextEntity()) {
+          //get a reference to the dog
+          var dog = dogs.getNextEntity();
+          var name = dog.get('name');
+          notice('dog is called ' + name);
+        }
+        dogs.resetEntityPointer();
+        //do doggy cleanup
+        while(dogs.hasNextEntity()) {
+          //get a reference to the dog
+          var dog = dogs.getNextEntity();
+          var dogname = dog.get('name');
+          notice('removing dog ' + dogname + ' from database');
+          dog.destroy(function(err, data) {
+            if (err) {
+              error('dog not removed');
+            } else {
+              success('dog removed');
+            }
+          });
+        }
+
+        //no need to wait around for dogs to be removed, so go on to next test
+        runner(step);
+      }
+    });
+  }
+//var counter;
+function counterCreate(step){
+	var counter = new usergrid.counter({client:client, data:{category:'mocha_test', timestamp:0, name:"test", counters:{test:0,test_counter:0}}}, function(err, data){
+        if (err) {
+          error('counter not removed');
+        } else {
+          success('counter created');
+        }
+	});
+	runner(step, counter);
+}
+function counterReset(step, counter){
+	counter.reset({name:'test'}, function(err, data){
+        if (err) {
+          error('counter not reset');
+        } else {
+          success('counter reset');
+        }
+		runner(step, counter);
+	});
+}
+function counterIncrement(step, counter){
+	counter.increment({name:'test', value:1}, function(err, data){
+        if (err) {
+          error('counter not incremented');
+        } else {
+          success('counter incremented');
+        }
+		runner(step, counter);
+	});
+}
+function counterDecrement(step, counter){
+	counter.decrement({name:'test', value:1}, function(err, data){
+        if (err) {
+          error('counter not decremented');
+        } else {
+          success('counter decremented');
+        }
+		runner(step, counter);
+	});
+}
+function counterFetch(step, counter){
+	counter.getData({resolution:'all', counters:['test', 'test_counter']}, function(err, data){
+        if (err) {
+          error('counter not fetched');
+        } else {
+          success('counter fetched');
+        }
+		runner(step, counter);
+	});
+}

http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/blob/7b92555b/test/client.js
----------------------------------------------------------------------
diff --git a/test/client.js b/test/client.js
new file mode 100755
index 0000000..70f8d85
--- /dev/null
+++ b/test/client.js
@@ -0,0 +1,183 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF 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.
+//
+
+
+/**
+* Test suite for Client object
+*
+* @author rod simpson (rod@apigee.com)
+*/
+//require('assert');
+require('should');
+var usergrid = require('../lib/usergrid.js');
+
+//first set up the client
+var myclient = new usergrid.client(
+  {
+    orgName:"1hotrod"
+  , appName:"sandbox"
+  , authType:"CLIENT_ID"
+  , clientId:"b3U6y6hRJufDEeGW9hIxOwbREg"
+  , clientSecret:"b3U6X__fN2l9vd1HVi1kM9nJvgc-h5k"
+  }
+);
+
+describe('Standard Requests', function(){
+  describe('DELETE Method', function(){
+    it('should DELETE without error', function(done){
+      myclient.request(
+        { method:"DELETE"
+        , endpoint:"users/aaaaaa"
+        } ,done);
+    });
+  });
+  describe('cURL DELETE Method', function(){
+    it('should create a valid cURL calll for the DELETE without error', function(){
+      var options = {
+        method:"DELETE"
+        , uri:"https://api.usergrid.com/1hotrod/sandbox/users/aaaaaa"
+      }
+      var curl = myclient.buildCurlCall(options);
+      curl.should.equal('curl -X DELETE https://api.usergrid.com/1hotrod/sandbox/users/aaaaaa');
+    });
+  });
+
+
+  describe('POST Method', function(){
+    it('should POST without error', function(done){
+      myclient.request(
+        { method:"POST"
+        , endpoint:"users"
+        , body:{'username':'aaaaaa', 'password':'abcd1234'}
+        } ,done);
+    })
+  });
+  describe('cURL POST Method', function(){
+    it('should create a valid cURL calll for the POST without error', function(){
+      var options = {
+        method:"POST"
+        , uri:"https://api.usergrid.com/1hotrod/sandbox/users"
+        , body:{'username':'aaaaaa', 'password':'abcd1234'}
+      }
+      var curl = myclient.buildCurlCall(options);
+      curl.should.equal("curl -X POST https://api.usergrid.com/1hotrod/sandbox/users -d '{\"username\":\"aaaaaa\",\"password\":\"abcd1234\"}'");
+    });
+  });
+
+
+  describe('PUT Method', function(){
+    it('should PUT without error', function(done){
+      myclient.request(
+        { method:"PUT"
+        , endpoint:"users/aaaaaa"
+        , body:{'fred':'value'}
+        } ,done);
+    });
+  });
+  describe('cURL PUT Method', function(){
+    it('should create a valid cURL calll for the PUT without error', function(){
+      var options = {
+        method:"PUT"
+        , uri:"https://api.usergrid.com/1hotrod/sandbox/users"
+        , body:{'fred':'value'}
+      }
+      var curl = myclient.buildCurlCall(options);
+      curl.should.equal("curl -X PUT https://api.usergrid.com/1hotrod/sandbox/users -d '{\"fred\":\"value\"}'");
+    });
+  });
+
+
+  describe('GET Method', function(){
+    it('should GET without error', function(done){
+      myclient.request(
+        { method:"GET"
+        , endpoint:"users/aaaaaa"
+        } ,done);
+    });
+  });
+  describe('cURL GET Method', function(){
+    it('should create a valid cURL calll for the GET without error', function(){
+      var options = {
+        method:"GET"
+        , uri:"https://api.usergrid.com/1hotrod/sandbox/users/aaaaaa"
+      }
+      var curl = myclient.buildCurlCall(options);
+      curl.should.equal('curl -X GET https://api.usergrid.com/1hotrod/sandbox/users/aaaaaa');
+    });
+  });
+
+  describe('Login Method', function(){
+    it('should Login without error and get token', function(done){
+      myclient.login('aaaaaa', 'abcd1234', function(err){
+        if (err) throw err;
+
+        //test the token first
+        var token = myclient.token;
+        myclient.should.have.property('token');
+
+        //make sure we get a user back
+        var user = myclient.user;
+        var data = user.get();
+        data.should.have.property('username');
+
+        //test for logged in user
+        if (!myclient.isLoggedIn()) throw err;
+
+        //make a query with the app users token
+        myclient.authType = usergrid.APP_USER;
+
+        //do a get on /users
+        describe('GET Method', function(){
+          it('should GET without error', function(done){
+            myclient.request(
+              { method:"GET"
+              , endpoint:"users"
+              } ,done);
+          });
+        });
+
+        //go back to the
+        myclient.authType = usergrid.AUTH_CLIENT_ID;
+
+        //erase the token
+        myclient.token = null;
+        if (myclient.isLoggedIn()) throw err;
+
+        //reset the token
+        myclient.token = token;
+        if (!myclient.isLoggedIn()) throw err;
+
+        //clear the logged in user
+        myclient.user = null;
+        if (myclient.isLoggedIn()) throw err;
+
+        //replace the logged in user
+        myclient.user = user;
+        if (!myclient.isLoggedIn()) throw err;
+
+        //log the user out
+        myclient.logout();
+        if (myclient.isLoggedIn()) throw err;
+
+        //tests finished
+        done();
+      });
+    });
+  })
+
+});
+

http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/blob/7b92555b/test/collection.js
----------------------------------------------------------------------
diff --git a/test/collection.js b/test/collection.js
new file mode 100755
index 0000000..1a8eadb
--- /dev/null
+++ b/test/collection.js
@@ -0,0 +1,259 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF 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.
+//
+
+/**
+* Test suite for Collection object
+*
+* TODO: need to add coverage for the following methods:
+*
+* getFirstEntity
+* getLastEntity
+* hasPrevEntity
+* getPrevEntity
+* resetEntityPointer
+* resetPaging
+*
+* Need to add sample data for paging, check actual results
+*
+* @author rod simpson (rod@apigee.com)
+*/
+require("assert");
+require('should');
+var usergrid = require('../lib/usergrid.js');
+
+//first set up the client
+var myclient = new usergrid.client({
+  orgName:'1hotrod',
+  appName:'sandbox',
+  authType:'CLIENT_ID',
+  clientId:'b3U6y6hRJufDEeGW9hIxOwbREg',
+  clientSecret:'b3U6X__fN2l9vd1HVi1kM9nJvgc-h5k',
+  logging: true
+});
+
+describe('Collection methods - dogs', function(){
+  var doggies = {};
+
+  describe('make new collection', function(){
+    it('should make a new collection without error', function(done){
+      var options = {
+        client:myclient,
+        path:"dogs"
+      }
+      doggies = new usergrid.collection(options, done);
+    });
+  });
+
+  describe('check collection', function(){
+    it('should loop through all collection entities', function(){
+      while(doggies.hasNextEntity()) {
+        //get a reference to the dog
+        var dog = doggies.getNextEntity();
+        var data = dog.get();
+        data.should.have.property('name');
+      }
+    });
+  });
+});
+
+describe('Collection methods - users', function(){
+  var users = {};
+
+  describe('make new collection', function(){
+    it('should make a new collection without error', function(done){
+      var options = {
+        client:myclient,
+        path:'users'
+      }
+      users = new usergrid.collection(options, done);
+    });
+  });
+
+  describe('check collection', function(){
+    it('should loop through all collection entities', function(){
+      while(users.hasNextEntity()) {
+        //get a reference to the dog
+        var user = users.getNextEntity();
+        var data = user.get();
+        data.should.have.property('username');
+      }
+    });
+  });
+});
+
+describe('Collection methods - 1 user - barney', function(){
+  var users = {};
+  var uuid = '';
+  var user_barney = {};
+
+  describe('make new collection', function(){
+    it('should make a new collection without error', function(done){
+      var options = {
+        client:myclient,
+        path:'users',
+        qs:{"ql":"select * where username ='barney'"}
+      }
+      users = new usergrid.collection(options, done);
+    });
+  });
+
+  describe('check collection', function(){
+    it('should loop through all collection entities', function(){
+      while(users.hasNextEntity()) {
+        //get a reference to the dog
+        var user = users.getNextEntity();
+        var data = user.get();
+        data.should.have.property('username', 'barney');
+      }
+    });
+  });
+
+  describe('Add 1 user to collection', function(){
+    it('should make a new user and add it to collection without error', function(done){
+      //first delete the user if he exists (no assertion as the data may or may not be there)
+      myclient.request({
+        method:'DELETE',
+        endpoint:'users/fredflintster'
+        }, function(err) {
+          /// new entity creation
+          var data = {
+          	type:'users',
+            username: 'fredflintster',
+            password: 'barney',
+            email: 'email@myemail.com'
+          };
+          var options = {
+            client:myclient,
+            data:data
+          };
+          user_barney = new Entity(options);
+          users.addEntity(user_barney, done);
+        });
+    });
+  });
+
+  describe('Get 1 user from collection', function(){
+    it('should return user without error', function(done){
+      //make sure we get the uuid from barney
+      var data = user_barney.get();
+      data.should.have.property('uuid');
+
+      var uuid =  user_barney.get('uuid');
+      users.getEntityByUUID(uuid, function(err, data, user) {
+        user_barney = user;
+        var data = user_barney.get();
+        data.should.have.property('uuid');
+        uuid = user_barney.get('uuid');
+        done();
+      });
+    });
+  });
+
+  describe('remove entity from collection', function(){
+    it('should remove entity from collection without error', function(done){
+      users.destroyEntity(user_barney, done);
+    });
+  });
+
+});
+
+
+
+
+
+var messageeClient = new usergrid.client({
+  orgName:'apigee',
+  appName:'messageeapp',
+  authType:'CLIENT_ID',
+  clientId:'YXA6URHEY2pCEeG23RIxOAoChA',
+  clientSecret:'YXA6ukLeZvwB0JOdmAprY1azi9DtCPY',
+  logging: true
+});
+
+describe('Collection methods - users paging', function(){
+  var users = {};
+
+  describe('make new collection', function(){
+    it('should make a new collection without error', function(done){
+      var options = {
+        path:'users',
+        client:messageeClient
+      }
+      users = new usergrid.collection(options, done);
+    });
+  });
+
+  describe('check collection', function(){
+    it('should loop through all collection entities', function(){
+      while(users.hasNextEntity()) {
+        //get a reference to the dog
+        var user = users.getNextEntity();
+        var data = user.get();
+        data.should.have.property('username');
+        console.log(data.username);
+      }
+    });
+  });
+
+  describe('get next page', function(){
+    it('should get next page of users', function(done){
+      console.log('starting next page test');
+      if (users.hasNextPage()) {
+        console.log('next page - yes');
+        users.getNextPage(done);
+      } else {
+        done();
+      }
+    });
+  });
+
+  describe('check collection', function(){
+    it('should loop through all collection entities', function(){
+      while(users.hasNextEntity()) {
+        //get a reference to the dog
+        var user = users.getNextEntity();
+        var data = user.get();
+        data.should.have.property('username');
+        console.log(data.username);
+      }
+    });
+  });
+
+  describe('get previous page', function(){
+    it('should get previous page of users', function(done){
+      if (users.hasPreviousPage()) {
+        users.getPreviousPage(done);
+      } else {
+        done();
+      }
+    });
+  });
+
+  describe('check collection', function(){
+    it('should loop through all collection entities', function(){
+      while(users.hasNextEntity()) {
+        //get a reference to the dog
+        var user = users.getNextEntity();
+        var data = user.get();
+        data.should.have.property('username');
+        console.log(data.username);
+      }
+    });
+  });
+
+});
+

http://git-wip-us.apache.org/repos/asf/usergrid-nodejs/blob/7b92555b/test/entity.js
----------------------------------------------------------------------
diff --git a/test/entity.js b/test/entity.js
new file mode 100755
index 0000000..3cd3b2e
--- /dev/null
+++ b/test/entity.js
@@ -0,0 +1,59 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF 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.
+//
+
+/**
+* Test suite for Entity object
+*
+* Run with mocha v. 1.7.x
+* http://visionmedia.github.com/mocha/
+*
+* @author rod simpson (rod@apigee.com)
+*/
+require("assert");
+var usergrid = require('../lib/usergrid.js');
+
+//first set up the client
+var client = new usergrid.client({
+  orgName:'1hotrod',
+  appName:'sandbox',
+  authType:'CLIENT_ID',
+  clientId:'b3U6y6hRJufDEeGW9hIxOwbREg',
+  clientSecret:'b3U6X__fN2l9vd1HVi1kM9nJvgc-h5k'
+});
+
+describe('Entity methods', function(){
+  var dog = new usergrid.entity({
+    client:client,
+    data:{type:"dogs"}
+  });
+  describe('save method', function(){
+    it('should save without error', function(done){
+      dog.set('name','dougy');
+      dog.save(done);
+    });
+  });
+  describe('fetch method', function(){
+    it('should fetch without error', function(done){
+      dog.fetch(done);
+    });
+  });
+  describe('destroy method', function(){
+    it('should destroy without error', function(done){
+      dog.destroy(done);
+    });
+  });
+});
\ No newline at end of file


Mime
View raw message