-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
feat(node): add sdk methods for organization apis #4826
Changes from 3 commits
9bebfed
e7823b8
ef20b05
9b9fbb3
7bd8cfd
c5049e3
2519732
45afac7
28f156b
a136ad3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
export interface IOrganizations { | ||
list(); | ||
create(payload: IOrganizationCreatePayload); | ||
rename(payload: IOrganizationRenamePayload); | ||
getCurrent(); | ||
removeMember(memberId: string); | ||
updateMemberRole( | ||
memberId: string, | ||
payload: IOrganizationUpdateMemberRolePayload | ||
); | ||
Comment on lines
+6
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These two methods also may cause problems when full Role-based access control will be implemented? I can imagine member that may bypass role-based access control by using API KEY to remove other members or update their role - they will do it as "organization owner" (assuming that member would be able to see api key). |
||
getMembers(); | ||
updateBranding(payload: IOrganizationBrandingPayload); | ||
} | ||
|
||
export interface IOrganizationCreatePayload { | ||
name: string; | ||
logo?: string; | ||
} | ||
|
||
export interface IOrganizationRenamePayload { | ||
name: string; | ||
} | ||
|
||
export interface IOrganizationUpdateMemberRolePayload { | ||
role: string; | ||
} | ||
|
||
export interface IOrganizationBrandingPayload { | ||
logo: string; | ||
color: string; | ||
fontColor?: string; | ||
contentBackground?: string; | ||
fontFamily: string; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
import { Novu } from '../novu'; | ||
import axios from 'axios'; | ||
|
||
const mockConfig = { | ||
apiKey: '1234', | ||
}; | ||
|
||
const mockedOrganization = { | ||
_id: '649070af750b25b4ac8a4704', | ||
__v: 0, | ||
name: 'Test Organization', | ||
branding: { | ||
logo: 'https://s3.us-east-1.amazonaws.com/bucket/key.jpeg', | ||
color: '#ff5517', | ||
fontFamily: 'Lato', | ||
}, | ||
createdAt: '2023-06-19T15:13:51.961Z', | ||
updatedAt: '2023-06-19T15:13:51.966Z', | ||
}; | ||
|
||
const mockedMember = { | ||
_id: '649070af750b25b4ac8a4759', | ||
memberStatus: 'active', | ||
_userId: '649070afaa9e50289df420d8', | ||
roles: ['admin'], | ||
_organizationId: mockedOrganization._id, | ||
createdAt: mockedOrganization.createdAt, | ||
updatedAt: mockedOrganization.createdAt, | ||
__v: 0, | ||
id: '649070af750b25b4ac8a4759', | ||
user: { | ||
_id: '649070afaa9e50289df420d8', | ||
firstName: 'john', | ||
lastName: 'doe', | ||
email: '[email protected]', | ||
profilePicture: | ||
'https://gravatar.com/avatar/fd876f8cd6a58277fc664d47ea10ad19?d=mp', | ||
createdAt: '2023-03-07T13:32:54.573Z', | ||
id: '649070afaa9e50289df420d8', | ||
}, | ||
}; | ||
|
||
jest.mock('axios'); | ||
|
||
describe('Novu Node.js package - Organizations class', () => { | ||
const mockedAxios = axios as jest.Mocked<typeof axios>; | ||
let novu: Novu; | ||
|
||
const methods = ['get', 'post', 'put', 'delete', 'patch']; | ||
|
||
beforeEach(() => { | ||
mockedAxios.create.mockReturnThis(); | ||
novu = new Novu(mockConfig.apiKey); | ||
}); | ||
|
||
afterEach(() => { | ||
methods.forEach((method) => { | ||
mockedAxios[method].mockClear(); | ||
}); | ||
}); | ||
|
||
it('should list organizations', async () => { | ||
const mockedResponse = { | ||
data: [mockedOrganization], | ||
}; | ||
mockedAxios.get.mockResolvedValue(mockedResponse); | ||
|
||
const result = await novu.organizations.list(); | ||
|
||
expect(mockedAxios.get).toBeCalled(); | ||
expect(result).toStrictEqual(mockedResponse); | ||
}); | ||
|
||
it('should create new organization', async () => { | ||
const organizationName = 'New Organization'; | ||
const mockedResponse = { | ||
data: { | ||
...mockedOrganization, | ||
name: organizationName, | ||
}, | ||
}; | ||
mockedAxios.post.mockResolvedValue(mockedResponse); | ||
|
||
const payload = { name: organizationName }; | ||
const result = await novu.organizations.create(payload); | ||
|
||
expect(mockedAxios.post).toBeCalledWith('/organizations', payload); | ||
expect(result).toStrictEqual(mockedResponse); | ||
}); | ||
|
||
it('should rename current organization', async () => { | ||
const newName = 'Renamed Organization'; | ||
const mockedResponse = { | ||
data: { | ||
name: newName, | ||
}, | ||
}; | ||
mockedAxios.patch.mockResolvedValue(mockedResponse); | ||
|
||
const payload = { name: newName }; | ||
const result = await novu.organizations.rename(payload); | ||
|
||
expect(result).toStrictEqual(mockedResponse); | ||
expect(mockedAxios.patch).toBeCalledWith('/organizations', payload); | ||
}); | ||
|
||
it('should fetch current organization', async () => { | ||
const mockedResponse = { data: mockedOrganization }; | ||
mockedAxios.get.mockResolvedValue(mockedResponse); | ||
|
||
const result = await novu.organizations.getCurrent(); | ||
|
||
expect(result).toStrictEqual(mockedResponse); | ||
expect(mockedAxios.get).toBeCalledWith('/organizations/me'); | ||
}); | ||
|
||
it('should remove member from current organization', async () => { | ||
const mockedResponse = { data: mockedMember }; | ||
mockedAxios.delete.mockResolvedValue(mockedResponse); | ||
|
||
const result = await novu.organizations.removeMember(mockedMember.id); | ||
|
||
expect(result).toStrictEqual(mockedResponse); | ||
expect(mockedAxios.delete).toBeCalledWith( | ||
`/organizations/members/${mockedMember.id}` | ||
); | ||
}); | ||
|
||
it('should update member role in current organization', async () => { | ||
const mockedResponse = { data: mockedMember }; | ||
mockedAxios.put.mockResolvedValue(mockedResponse); | ||
|
||
const payload = { role: 'admin' }; | ||
const result = await novu.organizations.updateMemberRole( | ||
mockedMember.id, | ||
payload | ||
); | ||
|
||
expect(result).toStrictEqual(mockedResponse); | ||
expect(mockedAxios.put).toBeCalledWith( | ||
`/organizations/members/${mockedMember.id}/roles`, | ||
payload | ||
); | ||
}); | ||
|
||
it('should fetch all members of current organization', async () => { | ||
const mockedResponse = { data: [mockedMember] }; | ||
mockedAxios.get.mockResolvedValue(mockedResponse); | ||
|
||
const result = await novu.organizations.getMembers(); | ||
|
||
expect(result).toStrictEqual(mockedResponse); | ||
expect(mockedAxios.get).toBeCalledWith('/organizations/members'); | ||
}); | ||
|
||
it('should update branding details of current organization', async () => { | ||
const payload = { | ||
logo: 'https://s3.us-east-1.amazonaws.com/bucket/key.jpeg', | ||
color: '#000000', | ||
fontFamily: 'Lato', | ||
}; | ||
const mockedResponse = { data: payload }; | ||
mockedAxios.put.mockResolvedValue(mockedResponse); | ||
|
||
const result = await novu.organizations.updateBranding(payload); | ||
|
||
expect(result).toStrictEqual(mockedResponse); | ||
expect(mockedAxios.put).toBeCalledWith('/organizations/branding', payload); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { WithHttp } from '../novu.interface'; | ||
import { | ||
IOrganizations, | ||
IOrganizationCreatePayload, | ||
IOrganizationRenamePayload, | ||
IOrganizationUpdateMemberRolePayload, | ||
IOrganizationBrandingPayload, | ||
} from './organizations.interface'; | ||
|
||
export class Organizations extends WithHttp implements IOrganizations { | ||
list() { | ||
return this.http.get('/organizations'); | ||
} | ||
|
||
create(payload: IOrganizationCreatePayload) { | ||
return this.http.post('/organizations', payload); | ||
} | ||
|
||
rename(payload: IOrganizationRenamePayload) { | ||
return this.http.patch('/organizations', payload); | ||
} | ||
|
||
getCurrent() { | ||
return this.http.get('/organizations/me'); | ||
} | ||
|
||
removeMember(memberId: string) { | ||
return this.http.delete(`/organizations/members/${memberId}`); | ||
} | ||
|
||
updateMemberRole( | ||
memberId: string, | ||
payload: IOrganizationUpdateMemberRolePayload | ||
) { | ||
return this.http.put(`/organizations/members/${memberId}/roles`, payload); | ||
} | ||
|
||
getMembers() { | ||
return this.http.get('/organizations/members'); | ||
} | ||
|
||
updateBranding(payload: IOrganizationBrandingPayload) { | ||
return this.http.put('/organizations/branding', payload); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why these two methods are public? For me it doesn't make sense - API KEY is attached to single organization, so why we want to allow to cross that boundary?
Whose organizations will be returned? Organizations of the owner?
I'm just wondering - but I implemented that anyway.