Skip to content

Commit

Permalink
#347: Bring up to date with latest from #356
Browse files Browse the repository at this point in the history
Merging in recent changes from #356 so we can test the editing functionality on the backend.
  • Loading branch information
seidior committed Jun 3, 2022
2 parents 0fc6ef0 + 478c5b7 commit e50fc7f
Show file tree
Hide file tree
Showing 11 changed files with 417 additions and 25 deletions.
52 changes: 33 additions & 19 deletions api/src/middleware/__tests__/authRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,38 +75,52 @@ describe('authRouter', () => {
it('should redirect to the web app for a principal with a known GitHub user id', done => {
mockGetGithubAccessToken.mockResolvedValue('MOCK_ACCESS_TOKEN');
mockGetGithubUser.mockResolvedValue({ id: 1000 });
// TODO: fix later
// mockFindGithubUserByGithubId.mockResolvedValue({ principal_id: 1 });
// appAgent
// .get('/auth/github/callback?code=MOCK_CODE')
// .expect('Location', process.env.WEBAPP_ORIGIN)
// .expect(302, () => {
// expect(getGithubAccessToken).toHaveBeenCalledWith('MOCK_CODE');
// expect(getGithubUser).toHaveBeenCalledWith('MOCK_ACCESS_TOKEN');
// expect(mockFindGithubUserByGithubId).toHaveBeenCalledWith(1000);
// expect(mockCreateGithubUser).not.toHaveBeenCalled();
// done();
// });
mockFindGithubUserByGithubId.mockResolvedValue({
github_id: 1000,
username: '',
full_name: '',
avatar_url: '',
bio: '',
principal_id: 10,
});
appAgent
.get('/auth/github/callback?code=MOCK_CODE')
.expect('Location', process.env.WEBAPP_ORIGIN)
.expect(302, () => {
expect(getGithubAccessToken).toHaveBeenCalledWith('MOCK_CODE');
expect(getGithubUser).toHaveBeenCalledWith('MOCK_ACCESS_TOKEN');
expect(mockFindGithubUserByGithubId).toHaveBeenCalledWith(1000);
expect(mockCreateGithubUser).not.toHaveBeenCalled();
done();
});
});

it('should insert a new principal and GitHub user and then redirect to the web app for a principal with an unknown GitHub user id', done => {
mockGetGithubAccessToken.mockResolvedValue('MOCK_ACCESS_TOKEN');
mockGetGithubUser.mockResolvedValue({ id: 1000 });
mockFindGithubUserByGithubId.mockResolvedValue(null);
// TODO: fix later
// mockCreateGithubUser.mockResolvedValue({
// id: 10,
// github_id: 1000,
// principal_id: 1,
// });
mockCreateGithubUser.mockResolvedValue({
github_id: 1000,
username: '',
full_name: '',
avatar_url: '',
bio: '',
principal_id: 10,
});
appAgent
.get('/auth/github/callback?code=MOCK_CODE')
.expect('Location', process.env.WEBAPP_ORIGIN)
.expect(302, () => {
expect(getGithubAccessToken).toHaveBeenCalledWith('MOCK_CODE');
expect(getGithubUser).toHaveBeenCalledWith('MOCK_ACCESS_TOKEN');
expect(mockFindGithubUserByGithubId).toHaveBeenCalledWith(1000);
expect(mockCreateGithubUser).toHaveBeenCalledWith(1000);
expect(mockCreateGithubUser).toHaveBeenCalledWith({
github_id: 1000,
username: '',
full_name: '',
avatar_url: '',
bio: ''
});
done();
});
});
Expand Down
Empty file.
Empty file.
52 changes: 51 additions & 1 deletion api/src/middleware/profileRouter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Router } from 'express';
import { getUserProfileData } from '../services/getUserProfileDataService';
import { collectionEnvelope } from './responseEnvelope';
import { collectionEnvelope, itemEnvelope } from './responseEnvelope';
import { compareAndUpdatePrincipals, parsePutSubmission } from '../services/principalsService';

const profileRouter = Router();

Expand Down Expand Up @@ -43,4 +44,53 @@ profileRouter.get('/:principal_id', async (req, res, next) => {
}
});

profileRouter.put('/:principal_id', async (req, res, next) => {
const { principal_id } = req.params;
const path_principal_id: number = parseInt(principal_id);
const { principalId } = req.session;
const session_principal_id: number = principalId;

try {
// test to make sure we have a principal ID defined
if (principal_id == null) {
throw new Error(
'I was not passed a principal_id for which to show profile data.'
);
}

// test to make sure the session principal ID matches the principal ID in path
if (path_principal_id !== session_principal_id) {
res.status(403);
throw new Error('Session user does not match owner of profile.');
}

// parse the object we receive in the request into a well-formed IUserData object
// submitted_user is a validated IUserData object that we received from the request
const submitted_user_data = itemEnvelope(req.body);
const submitted_user = parsePutSubmission(submitted_user_data, path_principal_id);

// get the appropriate existing IUserData for that principal for comparison's sake
const path_user_profile_data = await getUserProfileData(principalId);

// send the object to a function that will compare the IUserData object to the existing one
// and update the database accordingly.
const modified_data = compareAndUpdatePrincipals(path_user_profile_data, submitted_user);
if (modified_data) {
res.status(200);
res.json(collectionEnvelope([await getUserProfileData(principalId)], 1));
} else {
// Leaving this redundancy in place here in case we decide to implement
// different behavior in the future.
res.status(200);
res.json(collectionEnvelope([await getUserProfileData(principalId)], 1));
}

// send back the appropriate HTTP status code and appropriate response to the requester
} catch (err) {
console.error('err', err);
next(err);
return;
}
});

export default profileRouter;
1 change: 1 addition & 0 deletions api/src/models/user_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface ISocialNetwork {
protocol: string;
base_url: string;
}

export interface ISocialProfile {
network_name: string;
user_name: string;
Expand Down
51 changes: 46 additions & 5 deletions api/src/services/__tests__/githubUsersService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,36 @@ import { mockQuery } from '../mockDb';

describe('githubUsersService', () => {
describe('findGithubUserByGithubId', () => {
it('should return the github user with the given github id', async () => {
const githubId = 1000;
const principalId = 10;
mockQuery(
'select `github_id`, `username`, `full_name`, `bio`, `avatar_url`, `principal_id` from `github_users` where `github_id` = ?',
[githubId],
[{ principal_id: principalId }]
);
expect(await findGithubUserByGithubId(githubId)).toEqual({
github_id: 1000,
username: '',
full_name: '',
avatar_url: '',
bio: '',
principal_id: 2,
});
});

it('should return null if no github user exists with the given github id', async () => {
const githubId = 1000;
mockQuery(
'select `id`, `principal_id` from `github_users` where `github_id` = ?',
[githubId],
[]
);
expect(await findGithubUserByGithubId(githubId)).toEqual(null);
});
});

describe('findGithubUsersByPrincipalId', () => {
it('should return the github user with the given github id', async () => {
const githubId = 1000;
const githubUserId = 1;
Expand Down Expand Up @@ -49,11 +79,22 @@ describe('githubUsersService', () => {
[githubUserId]
);
mockQuery('COMMIT;');
// expect(await createGithubUser(githubId)).toEqual({
// id: githubUserId,
// github_id: githubId,
// principal_id: principalId,
// });
expect(
await createGithubUser({
github_id: 1000,
username: '',
full_name: '',
avatar_url: '',
bio: '',
})
).toEqual({
github_id: 1000,
username: '',
full_name: '',
avatar_url: '',
bio: '',
principal_id: 10,
});
});
});
});
Empty file.
Loading

0 comments on commit e50fc7f

Please sign in to comment.