-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adjust frontend; move api conversion to own module
- Loading branch information
1 parent
be03c07
commit de1d88e
Showing
5 changed files
with
136 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import * as _ from 'lodash'; | ||
import { User, UserResponse } from '../models'; | ||
import { parseUserData, encodeUserData } from './user'; | ||
|
||
/** | ||
* check if an object is a partial version of another object | ||
* | ||
* Verify that each key in `part` has the same value in `whole`, | ||
* but ignore any properties of `whole` that are ommitted in `part`. | ||
*/ | ||
const isPartialOf = <T>(part: Partial<T>, whole: T): boolean => { | ||
const picked = _.pick(whole, _.keys(part)); | ||
return _.isEqual(part, picked); | ||
}; | ||
|
||
const customMatchers = { | ||
/** expect an object to be a partial version of another object */ | ||
toBePartialOf: (matchersUtil) => ({ | ||
compare: <T>(actual: Partial<T>, expected: T) => { | ||
const pass = isPartialOf(actual, expected); | ||
return { pass }; | ||
} | ||
}) | ||
}; | ||
|
||
describe('user API conversion', () => { | ||
let user: User; | ||
let userResponse: UserResponse; | ||
|
||
beforeEach(() => { | ||
jasmine.addMatchers(customMatchers); | ||
}); | ||
|
||
beforeEach(() => { | ||
user = new User( | ||
1, | ||
'Hamlet', | ||
false, | ||
10000, | ||
false, | ||
true, | ||
); | ||
}); | ||
|
||
beforeEach(() => { | ||
userResponse = { | ||
id: 1, | ||
username: 'Hamlet', | ||
email: '[email protected]', | ||
download_limit: 10000, | ||
is_admin: false, | ||
saml: false, | ||
profile: { | ||
enable_search_history: true, | ||
} | ||
}; | ||
}); | ||
|
||
it('should convert a user response to a user object', () => { | ||
expect(parseUserData(userResponse)).toEqual(user); | ||
}); | ||
|
||
it('should convert a user to a user response object', () => { | ||
const encoded = encodeUserData(user); | ||
(expect(encoded) as any).toBePartialOf(userResponse); | ||
}); | ||
|
||
it('should define inverse functions', () => { | ||
const encoded = encodeUserData(user); | ||
const decoded = parseUserData(encoded as UserResponse); | ||
expect(decoded).toEqual(user); | ||
|
||
const parsed = parseUserData(userResponse); | ||
const unparsed = encodeUserData(parsed); | ||
// this has to be a partial match because User contains a subset of the information | ||
// in the API | ||
(expect(unparsed) as any).toBePartialOf(userResponse); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import * as _ from 'lodash'; | ||
import { User, UserResponse } from '../models'; | ||
|
||
/* Transforms backend user response to User object | ||
* | ||
* @param result User response data | ||
* @returns User object | ||
*/ | ||
export const parseUserData = (result: UserResponse): User => new User( | ||
result.id, | ||
result.username, | ||
result.is_admin, | ||
result.download_limit == null ? 0 : result.download_limit, | ||
result.saml, | ||
result.profile.enable_search_history, | ||
); | ||
|
||
/** | ||
* Transfroms User data to backend UserResponse object | ||
* | ||
* Because this is used for patching, the data can be partial | ||
* | ||
* @param data (partial) User object | ||
* @returns UserResponse object | ||
*/ | ||
export const encodeUserData = (data: Partial<User>): Partial<UserResponse> => { | ||
const changeKeys = { | ||
name: 'username', | ||
isAdmin: 'is_admin', | ||
downloadLimit: 'download_limit', | ||
isSamlLogin: 'saml', | ||
enableSearchHistory: 'profile.enable_search_history' | ||
}; | ||
|
||
const encoded = {}; | ||
|
||
_.keys(data).forEach(key => { | ||
const value = data[key]; | ||
const path = changeKeys[key] ? _.toPath(changeKeys[key]) : key; | ||
_.set(encoded, path, value); | ||
}); | ||
|
||
return encoded; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,5 +10,7 @@ export const mockUserResponse: UserResponse = { | |
email: '[email protected]', | ||
download_limit: 10000, | ||
saml: false, | ||
enable_search_history: true, | ||
profile: { | ||
enable_search_history: true, | ||
}, | ||
}; |