Skip to content

Commit

Permalink
more tests and minor changes
Browse files Browse the repository at this point in the history
gitlab happen to don't support fetching key by id for other user. So getKey method work only for authenicated user only
  • Loading branch information
Sonu Meena committed Mar 1, 2015
1 parent fc8b82f commit e465171
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 83 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
doc
spec/helpers/testConf.js
junitresults.xml
tmp
18 changes: 6 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,6 @@ var gUsers = new Users({
admin: true
});

/** List Users
* @params {Object} optn Option
* @params { Function} callback Callback
*/
gUsers.find(opts, function(err, response, results){
if(err){
//handle error
}else{
console.log( result);
}
});

// find all users
gUsers.find( function(err,response, results){})
Expand All @@ -63,13 +52,18 @@ gUsers.find(4, function(err, response,result){

```

## Documentation: find it on [wiki](https://github.com/sahilsk/Gitlab-YAAC/wiki)

API
-------------

### GitLab API

#### NOTE: Not all resources are implemented yet. Only highlighted ones are currently implemented.
> Not all resources are implemented yet. Only highlighted ones are currently implemented.
Currently implemented resources are:

- Users

#### Resources

Expand Down
2 changes: 1 addition & 1 deletion lib/_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var Agent = function(opt){
, json: true
}

debug( "Gitlab Url: '%s'", this.options.url);
// debug( "Gitlab Url: '%s'", this.options.url);

if( this.options['url'].indexOf("/api/v3") === -1){
this.options.url = url.resolve(this.options.url , "/api/v3/" );
Expand Down
34 changes: 10 additions & 24 deletions lib/users.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* @module Users
* `Users` Resource module
*
* @module Users
*
*/

Expand Down Expand Up @@ -115,21 +116,21 @@ Users.prototype.create = function ( params, callback){

/** Modify a user
*
* @param { Number} user id
* @param { Number} uid: user id
* @param { Object} newInfo : Information to update
* @param { requestCallback } callback: Callback
*
*/

Users.prototype.update = function (id, newInfo, callback){
Users.prototype.update = function (uid, newInfo, callback){

if( typeof callback !== "function" || typeof id !== "number"){
if( typeof callback !== "function" || typeof uid !== "number"){
throw new Error("Invalid arguments");
}

var optionF = {
"method" : "PUT",
"path" : "users/" + id
"path" : "users/" + uid
}


Expand Down Expand Up @@ -256,46 +257,31 @@ Users.prototype.listKeys = function ( uid, callback){
* @param { number} keyid : SSH key id
* @param { requestCallback } callback: Callback
*
* NOTE : Used only to get current authenticated user key only.
*/

Users.prototype.getKey = function ( uid, keyId, callback){
Users.prototype.getKey = function ( keyId, callback){
var args = Array.prototype.slice.call(arguments);

if( arguments.length < 2){
if( arguments.length < 2 || arguments.length > 2){
throw new Error("Invalid arguments");
}

if( arguments.length === 2){
keyId = args[0];
callback = args[1];
uid = null;

}
if( typeof callback !== "function" || typeof keyId !== "number" ){
throw new Error("Invalid arguments");
}

if( uid){
if( typeof uid !== "number"){
throw new Error("Invalid user id");
}
}


var optionF = {
"method" : "GET",
"path" : "user/keys/" + keyId
}

if(uid)
optionF.path = "users/" +uid + "/keys/" + keyId ;


debug("finding key:---------------- ");
this.agent.dial(optionF, callback);

}


/** Add single key for current user or specified user
*
* @param { number} [uid] : uid of user
Expand Down
99 changes: 53 additions & 46 deletions spec/users-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var existingUser = {};

describe(" Users: user input check", function() {
var gUsers = null;
gUsers = new Users( conf.gitlab );
gUsers = new Users( conf.gitrep );

it("should not accept empty arguments ", function() {
expect( function(){ gUsers.find(); } ).toThrow();
Expand All @@ -24,7 +24,7 @@ describe(" Users: fetch all users", function(){
callback = jasmine.createSpy("callback");

beforeAll( function(done){
gUsers = new Users( conf.gitlab );
gUsers = new Users( conf.gitrep );
gUsers.find( function(err, res, result){
callback(err, res, result);
done();
Expand Down Expand Up @@ -63,7 +63,7 @@ describe("users - search by invalid input", function(){
var callback = jasmine.createSpy("callback");

beforeAll( function(done){
gUsers = new Users( conf.gitlab );
gUsers = new Users( conf.gitrep );
gUsers.find("rDontMatchWithEmailOrUsername", function(err, res, result){
callback(err, res, result);
done();
Expand All @@ -85,8 +85,8 @@ describe("users: CREATE, READ, UPDATE, DELETE", function(){
"password" : "S3CR3TGoesHere" };

var newKeyObj = {
"key": "ssh-dss AAAAB3NzaC1kc3MAAACBAMLrhYgI3atfrSD6KDas1b/3n6R/HP+bLaHHX6oh+L1vg31mdUqK0Ac/NjZoQunavoyzqdPYhFz9zzOezCrZKjuJDS3NRK9rspvjgM0xYR4d47oNZbdZbwkI4cTv/gcMlquRy0OvpfIvJtjtaJWMwTLtM5VhRusRuUlpH99UUVeXAAAAFQCVyX+92hBEjInEKL0v13c/egDCTQAAAIEAvFdWGq0ccOPbw4f/F8LpZqvWDydAcpXHV3thwb7WkFfppvm4SZte0zds1FJ+Hr8Xzzc5zMHe6J4Nlay/rP4ewmIW7iFKNBEYb/yWa+ceLrs+TfR672TaAgO6o7iSRofEq5YLdwgrwkMmIawa21FrZ2D9SPao/IwvENzk/xcHu7YAAACAQFXQH6HQnxOrw4dqf0NqeKy1tfIPxYYUZhPJfo9O0AmBW2S36pD2l14kS89fvz6Y1g8gN/FwFnRncMzlLY/hX70FSc/3hKBSbH6C6j8hwlgFKfizav21eS358JJz93leOakJZnGb8XlWvz1UJbwCsnR2VEY8Dz90uIk1l/UqHkA= loic@call",
"title": "dummyKey"};
"key": "ssh-dss AAAAB3NzaC1kc3MAAACBAMLrhYgI3atfrSD6KDas1b/3n6R/HP+bLaHHX6oh+L1vg31mdUqK0Ac/NjZoQunavoyzqdPYhFz9zzOezCrZKjuJDS3NRK9rspvjgM0xYR4d47oNZbdZbwkI4cTv/gcMlquRy0OvpfIvJtjtaJWMwTLtM5VhRusRuUlpH99UUVeXAAAAFQCVyX+92hBEjInEKL0v13c/egDCTQAAAIEAvFdWGq0ccOPbw4f/F8LpZqvWDydAcpXHV3thwb7WkFfppvm4SZte0zds1FJ+SRofEq5YLdwgrwkMmIawa21Hr8Xzzc5zMHe6J4Nlay/rP4ewmIW7iFKNBEYb/yWa+ceLrs+TfR672TaAgO6o7iFrZ2D9SPao/IwvENzk/xcHu7YAAACAQFXQH6HQnxOrw4dqf0NqeKy1tfIPxYYUZhPJfo9O0AmBW2S36pD2l14kS89fvz6Y1g8gN/FwFnRncMzlLY/hX70FSc/3hKBSbH6C6j8hwlgFKfizav21eS358JJz93leOakJZnGb8XlWvz1UJbwCsnR2VEY8Dz90uIk1l/UqHkA= loicc@call",
"title": "dummSSHkey"};

var gUsers = null;
var callback = jasmine.createSpy("callback");
Expand Down Expand Up @@ -124,12 +124,10 @@ describe("users: CREATE, READ, UPDATE, DELETE", function(){

beforeAll( function(done){
gUsers = new Users( conf.gitrep );
gUsers.find(function(err, res, result){
gUsers.find( newUser.id, function(err, res, result){
callback(err, res, result);
done();
});
})
gUsers.find( newUser.id, function(err, res, result){
callback(err, res, result);
done();
});
});

describe("- find user by id", function(){
Expand All @@ -148,7 +146,7 @@ describe("users: CREATE, READ, UPDATE, DELETE", function(){
var callback = jasmine.createSpy("callback");

beforeAll( function(done){
gUsers = new Users( conf.gitlab );
gUsers = new Users( conf.gitrep );
gUsers.find(newUser.email, function(err, res, result){
callback(err, res, result);
done();
Expand All @@ -158,7 +156,7 @@ describe("users: CREATE, READ, UPDATE, DELETE", function(){
it("- user should be found", function(done){
expect( callback.calls.mostRecent().args[1]["statusCode"]).toEqual(200);
expect( typeof callback.calls.mostRecent().args[2] ).toEqual("object");
expect( typeof callback.calls.mostRecent().args[2]["name"] ).toBeDefined();
expect( callback.calls.mostRecent().args[2][0]["name"] ).toBeDefined();
done();
});

Expand All @@ -169,17 +167,18 @@ describe("users: CREATE, READ, UPDATE, DELETE", function(){
var callback = jasmine.createSpy("callback");

beforeAll( function(done){
gUsers = new Users( conf.gitlab );
gUsers = new Users( conf.gitrep );
gUsers.find(newUser.username, function(err, res, result){
callback(err, res, result);
done();
})
});

it("- user should be found", function(done){
expect( callback.calls.mostRecent().args[0]).toBeNull();
expect( callback.calls.mostRecent().args[1]["statusCode"]).toEqual(200);
expect( typeof callback.calls.mostRecent().args[2] ).toEqual("object");
expect( typeof callback.calls.mostRecent().args[2]["name"] ).toBeDefined();
expect( callback.calls.mostRecent().args[2][0]["name"] ).toBe( newUser.name);
done();
});

Expand All @@ -191,12 +190,14 @@ describe("users: CREATE, READ, UPDATE, DELETE", function(){
var gUsers = null;
var callback = jasmine.createSpy("callback");

var updatedUser = newUser;
var updatedUser = JSON.parse( JSON.stringify( newUser ) );
updatedUser.name ="dummyChangedName";
updatedUser.skype ="dummySkypeInfo";

beforeAll( function(done){
gUsers = new Users( conf.gitrep );
updatedUser.id = newUser.id;
console.log("updating user:xxxxxxxxxxxxxxxxx", updatedUser);
gUsers.update(updatedUser.id, updatedUser, function(err, res, result){
callback(err, res, result);
done();
Expand All @@ -214,11 +215,9 @@ describe("users: CREATE, READ, UPDATE, DELETE", function(){

});

describe("- Add a new SSH keys", function(){
describe("- Add new SHH keys", function(){
var gUsers = null;
var callback =null ;

callback = jasmine.createSpy("callback");
var callback = jasmine.createSpy("callback");

beforeAll( function(done){
gUsers = new Users( conf.gitrep );
Expand All @@ -230,22 +229,18 @@ describe("users: CREATE, READ, UPDATE, DELETE", function(){
});

it("- should add new key", function(){
var err = callback.calls.mostRecent().args[0]
var response = callback.calls.mostRecent().args[1]

var err = callback.calls.mostRecent().args[0];
var response = callback.calls.mostRecent().args[1];
expect(err).toBeNull();
expect(response.statusCode).toBe(201);
});

});

describe("- Get SSh keys.(if any)", function(){

describe("- Get SSH keys.(if any)", function(){

var gUsers = null;
var callback =null ;

callback = jasmine.createSpy("callback");
var callback = jasmine.createSpy("callback");

beforeAll( function(done){
gUsers = new Users( conf.gitrep );
Expand All @@ -263,10 +258,7 @@ describe("users: CREATE, READ, UPDATE, DELETE", function(){
expect(err).toBeNull();
expect(response.statusCode).toBe(200);
expect( typeof keys).toBe("object");
});

it("- should have key properties", function(){
var keys = callback.calls.mostRecent().args[2];
if( keys.length > 0){
expect( keys[0].id).toBeDefined();
expect( keys[0].title).toBeDefined();
Expand Down Expand Up @@ -330,16 +322,14 @@ describe("users: CREATE, READ, UPDATE, DELETE", function(){
describe("Current User: ", function(){
var newKeyObj = {
"key": "ssh-dss AAAAB3NzaC1kc3MAAACBAMLrhYgI3atfrSD6KDas1b/3n6R/HP+bLaHHX6oh+L1vg31mdUqK0Ac/NjZoQunavoyzqdPYhFz9zzOezCrZKjuJDS3NRK9rspvjgM0xYR4d47oNZbdZbwkI4cTv/gcMlquRy0OvpfIvJtjtaJWMwTLtM5VhRusRuUlpH99UUVeXAAAAFQCVyX+92hBEjInEKL0v13c/egDCTQAAAIEAvFdWGq0ccOPbw4f/F8LpZqvWDydAcpXHV3thwb7WkFfppvm4SZte0zds1FJ+Hr8Xzzc5zMHe6J4Nlay/rP4ewmIW7iFKNBEYb/yWa+ceLrs+TfR672TaAgO6o7iSRofEq5YLdwgrwkMmIawa21FrZ2D9SPao/IwvENzk/xcHu7YAAACAQFXQH6HQnxOrw4dqf0NqeKy1tfIPxYYUZhPJfo9O0AmBW2S36pD2l14kS89fvz6Y1g8gN/FwFnRncMzlLY/hX70FSc/3hKBSbH6C6j8hwlgFKfizav21eS358JJz93leOakJZnGb8XlWvz1UJbwCsnR2VEY8Dz90uIk1l/UqHkA= loic@call",
"title": "dummyKey"};
"title": "dummySSHkey"};



var gUsers = null;
var callback =null ;

callback = jasmine.createSpy("callback");
var callback = jasmine.createSpy("callback");

beforeAll( function(done){
gUsers = new Users( conf.gitlab );
gUsers = new Users( conf.gitrep );
gUsers.current( function(err, res, result){
callback(err, res, result);
done();
Expand All @@ -355,7 +345,7 @@ describe("Current User: ", function(){
expect( typeof user).toBe("object");
})

describe("- Gets SSh keys.(if any)", function(){
describe("- Gets all SSH keys.(if any)", function(){
var gUsers = null;
var callback =null ;

Expand Down Expand Up @@ -389,15 +379,13 @@ describe("Current User: ", function(){
});
});

describe("- Add a new SSH keys", function(){
describe("- Add SSH key", function(){
var gUsers = null;
var callback =null ;

callback = jasmine.createSpy("callback");
var callback = jasmine.createSpy("callback");

beforeAll( function(done){
gUsers = new Users( conf.gitrep );
gUsers.addKey( newKeyObj, function(err, res, result){
gUsers.addKey( newKeyObj, function(err, res, result){
newKeyObj.id = result.id;
callback(err, res, result);
done();
Expand All @@ -414,11 +402,30 @@ describe("Current User: ", function(){

});

describe("- Delete SSH keys", function(){
describe("- Fetch SSH key by id", function(){
var gUsers = null;
var callback =null ;
var callback = jasmine.createSpy("callback");

beforeAll( function(done){
gUsers = new Users( conf.gitrep );
gUsers.getKey( newKeyObj.id , function(err, res, result){
callback(err, res, result);
done();
})
});

callback = jasmine.createSpy("callback");
it("- SSH key should be found", function(done){
expect( callback.calls.mostRecent().args[1]["statusCode"]).toEqual(200);
expect( typeof callback.calls.mostRecent().args[2] ).toEqual("object");
expect( callback.calls.mostRecent().args[2]["title"] ).toBe( newKeyObj.title);
done();
});

});

describe("- Delete SSH key", function(){
var gUsers = null;
var callback = jasmine.createSpy("callback");

beforeAll( function(done){
gUsers = new Users( conf.gitrep );
Expand Down

0 comments on commit e465171

Please sign in to comment.