@@ -11,6 +11,7 @@ describe("Remote", function() {
1111 var reposPath = local ( "../repos/workdir" ) ;
1212 var url = "https://github.com/nodegit/test" ;
1313 var url2 = "https://github.com/nodegit/test2" ;
14+ var privateUrl = "git@github.com:nodegit/private" ;
1415
1516 function removeNonOrigins ( repo ) {
1617 return repo . getRemotes ( )
@@ -163,6 +164,63 @@ describe("Remote", function() {
163164 } ) ;
164165 } ) ;
165166
167+ it ( "can fetch from a private repository" , function ( ) {
168+ this . timeout ( 15000 ) ;
169+
170+ var repo = this . repository ;
171+ var remote = Remote . create ( repo , "private" , privateUrl ) ;
172+ var fetchOptions = {
173+ callbacks : {
174+ credentials : function ( url , userName ) {
175+ return NodeGit . Cred . sshKeyNew (
176+ userName ,
177+ path . resolve ( "./test/nodegit-test-rsa.pub" ) ,
178+ path . resolve ( "./test/nodegit-test-rsa" ) ,
179+ ""
180+ ) ;
181+ } ,
182+ certificateCheck : function ( ) {
183+ return 1 ;
184+ }
185+ }
186+ } ;
187+
188+ return remote . fetch ( null , fetchOptions , "Fetch from private" )
189+ . catch ( function ( ) {
190+ assert . fail ( "Unable to fetch from private repository" ) ;
191+ } ) ;
192+ } ) ;
193+
194+ it ( "can reject fetching from private repository without valid " +
195+ "credentials" , function ( ) {
196+ this . timeout ( 15000 ) ;
197+
198+ var repo = this . repository ;
199+ var remote = Remote . create ( repo , "private" , privateUrl ) ;
200+ var fetchOptions = {
201+ callbacks : {
202+ credentials : function ( url , userName ) {
203+ return NodeGit . Cred . sshKeyFromAgent ( userName ) ;
204+ } ,
205+ certificateCheck : function ( ) {
206+ return 1 ;
207+ }
208+ }
209+ } ;
210+
211+ return remote . fetch ( null , fetchOptions , "Fetch from private" )
212+ . then ( function ( ) {
213+ assert . fail ( "Should not be able to fetch from repository" ) ;
214+ } )
215+ . catch ( function ( error ) {
216+ assert . equal (
217+ error . message . trim ( ) ,
218+ "ERROR: Repository not found." ,
219+ "Should not be able to find repository."
220+ ) ;
221+ } ) ;
222+ } ) ;
223+
166224 it ( "can fetch from all remotes" , function ( ) {
167225 // Set a reasonable timeout here for the fetchAll test
168226 this . timeout ( 15000 ) ;
@@ -183,29 +241,17 @@ describe("Remote", function() {
183241 } ) ;
184242 } ) ;
185243
186- it ( "cannot push to a repository" , function ( ) {
244+ it ( "cannot push to a repository with invalid credentials " , function ( ) {
187245 this . timeout ( 5000 ) ;
188246 var repo = this . repository ;
189247 var branch = "should-not-exist" ;
190248 return Remote . lookup ( repo , "origin" )
191249 . then ( function ( remote ) {
192250 var ref = "refs/heads/" + branch ;
193251 var refs = [ ref + ":" + ref ] ;
194- var firstPass = true ;
195252 var options = {
196253 callbacks : {
197- credentials : function ( url , userName ) {
198- if ( firstPass ) {
199- firstPass = false ;
200- if ( url . indexOf ( "https" ) === - 1 ) {
201- return NodeGit . Cred . sshKeyFromAgent ( userName ) ;
202- } else {
203- return NodeGit . Cred . userpassPlaintextNew ( userName , "" ) ;
204- }
205- } else {
206- return NodeGit . Cred . defaultNew ( ) ;
207- }
208- } ,
254+ credentials : function ( url , userName , allowedTypes ) { } ,
209255 certificateCheck : function ( ) {
210256 return 1 ;
211257 }
0 commit comments