Skip to content

Commit 1422027

Browse files
committed
Implemented private remote tests
Added test to check if nodegit can fetch from a private repo with valid creds Added test to check if nodegit cannot fetch from private repo with invalid creds
1 parent b4bb72c commit 1422027

File tree

3 files changed

+76
-14
lines changed

3 files changed

+76
-14
lines changed

test/nodegit-test-rsa

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-----BEGIN RSA PRIVATE KEY-----
2+
MIICXQIBAAKBgQC5VMnDStyy3BDQ4Ilb/XnD5htRn0f3Is8NITe9o/KwZcPy2MFM
3+
RssnQjyp/WoQXl/o3Id+UaTZ+TKzYYmP/bGnYlyZP+FBBTKrCcDDW0fVjLbDazEX
4+
usAYCW58iGG9EomrwpSVrZlrLsZc1CPUdeeC8jmTuBQR7fyDFxM4sdoIyQIDAQAB
5+
AoGBAA7ZZ00Ze3NtRH+n7fmL5qy2jCvPClIX3OUdazzrnO9bSAb2LQ6ygWaE3R11
6+
q4DiPucGfGi3m/DHEbPjtcTuu8Qdqr7Nb0FU2CS2og0zMO+Z+CcyzIkXN0o7spig
7+
ekEY1Uml5MKGcJxu6afsOn+9LIi05SsYPCdtB5z716IewNgxAkEA2pKXov7jobMb
8+
2vBA4BtEtcryflWFO21uwMDHlJ34mjrUAMCX89NPrY//v8g0eYGLm9ZAj/Ik632+
9+
uOKHCXaG7QJBANkRBBcnKojqybJoxbL9PLJ3VoJ+EfLllGsbMHzc5VMwQF8ViwBD
10+
dOQ5feVKg601814Y1NGul/nprk896GNbUc0CQQCHD8iV1u1wcQ4IZyeflBoMQAFC
11+
YbQ3ebLTdwyc4FTLcQiqAlijOXNl67J8nskWQB+1x1oT2OxJfGVLN+d7yHstAkBz
12+
GKCwniXhn4z/OqrJc5mBj+GhI7PktXLzL+GP85jteUJIqKWhqCMM+KcWs2IKr/ax
13+
SD1gSVFwREYW4l6cgElpAkBtngXppGinh3nywIIo/SFmUJV/cUlWBi6MMgfsP8b6
14+
37+bqJI+m56WBdAG2xNz0uk6DIMp6R7JafOpfgOIPPk9
15+
-----END RSA PRIVATE KEY-----

test/nodegit-test-rsa.pub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQC5VMnDStyy3BDQ4Ilb/XnD5htRn0f3Is8NITe9o/KwZcPy2MFMRssnQjyp/WoQXl/o3Id+UaTZ+TKzYYmP/bGnYlyZP+FBBTKrCcDDW0fVjLbDazEXusAYCW58iGG9EomrwpSVrZlrLsZc1CPUdeeC8jmTuBQR7fyDFxM4sdoIyQ==

test/tests/remote.js

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)