Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Authentication context (resolves #94) #97

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 37 additions & 34 deletions src/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const wrapCallback = (func: { (callback: any): void }) => {

describe('git', () => {
test('create, push to, and clone a repo', async () => {
expect.assertions(11);
expect.assertions(12);

let lastCommit: string;

Expand All @@ -29,8 +29,11 @@ describe('git', () => {
fs.mkdirSync(srcDir, '0700');
fs.mkdirSync(dstDir, '0700');

const repos = new Git(repoDir, {
const repos = new Git<string>(repoDir, {
autoCreate: true,
authenticate: (opts) => {
willstott101 marked this conversation as resolved.
Show resolved Hide resolved
return 'my request context';
},
});
const port = Math.floor(Math.random() * ((1 << 16) - 1e4)) + 1e4;
const server = http
Expand All @@ -42,6 +45,8 @@ describe('git', () => {
process.chdir(srcDir);

repos.on('push', (push) => {
expect(push.context).toBe('my request context');

expect(push.repo).toBe('xyz/doom');
expect(push.commit).toBe(lastCommit);
expect(push.branch).toBe('master');
Expand Down Expand Up @@ -655,17 +660,16 @@ describe('git', () => {

const repos = new Git(repoDir, {
autoCreate: true,
authenticate: ({ type, repo, user }, next) => {
authenticate: async ({ type, repo, getUser }) => {
if (type === 'fetch' && repo === 'doom') {
user((username, password) => {
if (username == 'root' && password == 'root') {
next();
} else {
next(new Error('that is not the correct password'));
}
});
const [username, password] = await getUser();
if (username == 'root' && password == 'root') {
return;
} else {
throw new Error('that is not the correct password');
}
} else {
next(new Error('that is not the correct password'));
throw new Error('that is not the correct password');
}
},
});
Expand Down Expand Up @@ -721,25 +725,30 @@ describe('git', () => {
fs.mkdirSync(srcDir, '0700');
fs.mkdirSync(dstDir, '0700');

const repos = new Git(repoDir, {
interface Context {
username: string;
}

const repos = new Git<Context>(repoDir, {
autoCreate: true,
authenticate: ({ type, repo, user, headers }, next) => {
authenticate: async ({ type, repo, getUser, headers }) => {
if (type === 'fetch' && repo === 'doom') {
expect(headers['host']).toBeTruthy();
expect(headers['user-agent']).toBeTruthy();
expect(headers['accept']).toBeTruthy();
expect(headers['pragma']).toBeTruthy();
expect(headers['accept-encoding']).toBeTruthy();

user((username, password) => {
if (username == 'root' && password == 'root') {
next();
} else {
next(new Error('that is not the correct password'));
}
});
const [username, password] = await getUser();
if (username == 'root' && password == 'root') {
return {
username: username,
};
} else {
throw new Error('that is not the correct password');
}
} else {
next(new Error('that is not the correct password'));
throw new Error('that is not the correct password');
}
},
});
Expand Down Expand Up @@ -795,20 +804,14 @@ describe('git', () => {

const repos = new Git(repoDir, {
autoCreate: true,
authenticate: ({ type, repo, user }) => {
return new Promise(function (resolve, reject) {
if (type === 'fetch' && repo === 'doom') {
user((username, password) => {
if (username == 'root' && password == 'root') {
return resolve(void 0);
} else {
return reject('that is not the correct password');
}
});
} else {
return reject('that is not the correct password');
authenticate: async ({ type, repo, getUser }) => {
if (type === 'fetch' && repo === 'doom') {
const [username, password] = await getUser();
if (username == 'root' && password == 'root') {
return;
}
});
}
throw new Error('that is not the correct password');
},
});
const port = Math.floor(Math.random() * ((1 << 16) - 1e4)) + 1e4;
Expand Down
Loading