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

Feat/graph api module #127

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MicrosoftPartnerCenter } from './lib/microsoft-partnercenter'
import { MicrosoftGraphApi } from './lib/microsoft-graph-api'
import { MicrosoftGraphApi } from './lib/graph-api/microsoft-graph-api'
export default MicrosoftPartnerCenter

export { MicrosoftPartnerCenter, MicrosoftGraphApi }
Expand Down
53 changes: 53 additions & 0 deletions src/lib/graph-api/domains/domains.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Domains } from './domains'
import mockAxios from 'jest-mock-axios'
import { AxiosInstance } from 'axios'
import { DomainDnsRecord } from '../../types'

describe('Domains', () => {
let domains: Domains

const data = { id: 'example.com' }

beforeEach(() => (domains = new Domains(mockAxios as unknown as AxiosInstance)))

afterEach(() => mockAxios.reset())

it('creates a domain', async () => {
jest.spyOn(mockAxios, 'post').mockResolvedValue({ data })
await expect(domains.createDomain(data.id)).resolves.toEqual(data)
expect(mockAxios.post).toHaveBeenCalledWith('/domains', data)
})

it('gets a domain by id', async () => {
jest.spyOn(mockAxios, 'get').mockResolvedValue({ data })
await expect(domains.getDomain(data.id)).resolves.toEqual(data)
expect(mockAxios.get).toHaveBeenCalledWith(`/domains/${data.id}`)
})

it('updates a domain', async () => {
const update = { id: 'new.example.com' }
jest.spyOn(mockAxios, 'patch').mockResolvedValue({ data: update })
await expect(domains.updateDomain(data.id, update)).resolves.toEqual(update)
expect(mockAxios.patch).toHaveBeenCalledWith(`/domains/${data.id}`, update)
})

it('deletes a domain', async () => {
const res = { status: 204 }
jest.spyOn(mockAxios, 'delete').mockResolvedValue(res)
await expect(domains.deleteDomain(data.id)).resolves.toEqual(res)
expect(mockAxios.delete).toHaveBeenCalledWith(`/domains/${data.id}`)
})

it('verifies a domain', async () => {
jest.spyOn(mockAxios, 'post').mockResolvedValue({ data })
await expect(domains.verifyDomain(data.id)).resolves.toEqual(data)
expect(mockAxios.post).toHaveBeenCalledWith(`/domains/${data.id}/verify`)
})

it(`verifies a domain's DNS records`, async () => {
const records = [{ id: 'id', ttl: 3600 }] as DomainDnsRecord[]
jest.spyOn(mockAxios, 'get').mockResolvedValue({ data: { value: records } })
await expect(domains.getDomainVerificationDnsRecords(data.id)).resolves.toEqual(records)
expect(mockAxios.get).toHaveBeenCalledWith(`/domains/${data.id}/verificationDnsRecords`)
})
})
81 changes: 81 additions & 0 deletions src/lib/graph-api/domains/domains.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { AxiosInstance } from 'axios'
import { Domain, DomainDnsRecord } from '../../types'

export class Domains {
constructor(private readonly http: AxiosInstance) {}

/**
* Create a new domain
* https://learn.microsoft.com/en-us/graph/api/domain-post-domains?view=graph-rest-1.0&tabs=http
* @param domainName The fully qualified name of the domain
* @returns The created Domain object
*/
async createDomain(domainName: string): Promise<Domain> {
const { data } = await this.http.post('/domains', { id: domainName })
return data
}

/**
* List all domains
* https://learn.microsoft.com/en-us/graph/api/domain-list?view=graph-rest-1.0&tabs=http
* @returns An array of Domain objects
*/
async getAllDomains(): Promise<Domain[]> {
const { data } = await this.http.get('/domains')
return data.value
}

/**
* Get a specific domain
* https://learn.microsoft.com/en-us/graph/api/domain-get?view=graph-rest-1.0&tabs=http
* @param domainId The domain ID (which is the fully qualified domain name)
* @returns The Domain object
*/
async getDomain(domainId: string): Promise<Domain> {
const { data } = await this.http.get(`/domains/${domainId}`)
return data
}

/**
* Update a domain
* https://learn.microsoft.com/en-us/graph/api/domain-update?view=graph-rest-1.0&tabs=http
* @param domainId The domain ID (which is the fully qualified domain name)
* @param updateData The data to update on the domain
* @returns The updated Domain object
*/
async updateDomain(domainId: string, updateData: Partial<Domain>): Promise<Domain> {
const { data } = await this.http.patch(`/domains/${domainId}`, updateData)
return data
}

/**
* Delete a domain
* https://learn.microsoft.com/en-us/graph/api/domain-delete?view=graph-rest-1.0&tabs=http
* @param domainId The domain ID (which is the fully qualified domain name)
*/
async deleteDomain(domainId: string): Promise<void> {
return this.http.delete(`/domains/${domainId}`)
}

/**
* Verify a domain
* https://learn.microsoft.com/en-us/graph/api/domain-verify?view=graph-rest-1.0&tabs=http
* @param domainId The domain ID (which is the fully qualified domain name)
* @returns The verified Domain object
*/
async verifyDomain(domainId: string): Promise<Domain> {
const { data } = await this.http.post(`/domains/${domainId}/verify`)
return data
}

/**
* Get verification DNS records for a domain
* https://learn.microsoft.com/en-us/graph/api/domain-list-verificationdnsrecords?view=graph-rest-1.0&tabs=http
* @param domainId The domain ID (which is the fully qualified domain name)
* @returns An array of DomainDnsRecord objects
*/
async getDomainVerificationDnsRecords(domainId: string): Promise<DomainDnsRecord[]> {
const { data } = await this.http.get(`/domains/${domainId}/verificationDnsRecords`)
return data.value
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
import { MicrosoftGraphApi } from './microsoft-graph-api'
import mockAxios from 'jest-mock-axios'
import {
CreateGDAPAccessAssignment,
CreateGDAPRelationship,
GDAPAccessAssignment,
GDAPRelationship,
UpdateGDAPAccessAssignment,
} from './types/gdap.types'
} from '../../types'
import { Gdap } from './gdap'
import { AxiosInstance } from 'axios'

describe('Microsoft Graph API', () => {
let graphApi: MicrosoftGraphApi
describe('Gdap', () => {
let gdap: Gdap

beforeEach(() => {
graphApi = new MicrosoftGraphApi({
tenantId: 'test',
authentication: {
clientId: 'test',
clientSecret: 'test',
},
})
gdap = new Gdap(mockAxios as never as AxiosInstance)
})

afterEach(() => {
Expand All @@ -31,7 +26,7 @@ describe('Microsoft Graph API', () => {
const data: CreateGDAPRelationship = {
customer: { tenantId: 'customerId' },
} as CreateGDAPRelationship
const result = await graphApi.createGDAPRelationship(data)
const result = await gdap.createGDAPRelationship(data)
expect(result).toEqual(relationship)
expect(mockAxios.post).toHaveBeenCalledWith(
'/tenantRelationships/delegatedAdminRelationships',
Expand All @@ -42,7 +37,7 @@ describe('Microsoft Graph API', () => {
it('should get all GDAP relationships', async () => {
const relationships: GDAPRelationship[] = [{ id: '1' }, { id: '2' }] as GDAPRelationship[]
jest.spyOn(mockAxios, 'get').mockResolvedValue({ data: { value: relationships } })
const result = await graphApi.getAllGDAPRelationships()
const result = await gdap.getAllGDAPRelationships()
expect(result).toEqual(relationships)
expect(mockAxios.get).toHaveBeenCalledWith(
'/tenantRelationships/delegatedAdminRelationships',
Expand All @@ -52,7 +47,7 @@ describe('Microsoft Graph API', () => {
it('should get a GDAP relationship', async () => {
const relationship: GDAPRelationship = { id: '1' } as GDAPRelationship
jest.spyOn(mockAxios, 'get').mockResolvedValue({ data: relationship })
const result = await graphApi.getGDAPRelationship('1')
const result = await gdap.getGDAPRelationship('1')
expect(result).toEqual(relationship)
expect(mockAxios.get).toHaveBeenCalledWith(
'/tenantRelationships/delegatedAdminRelationships/1',
Expand All @@ -62,7 +57,7 @@ describe('Microsoft Graph API', () => {
it('should get GDAP relationships by customer ID', async () => {
const relationships: GDAPRelationship[] = [{ id: '1' }, { id: '2' }] as GDAPRelationship[]
jest.spyOn(mockAxios, 'get').mockResolvedValue({ data: { value: relationships } })
const result = await graphApi.getGDAPRelationshipsByCustomerId('customerId')
const result = await gdap.getGDAPRelationshipsByCustomerId('customerId')
expect(result).toEqual(relationships)
expect(mockAxios.get).toHaveBeenCalledWith(
'/tenantRelationships/delegatedAdminRelationships',
Expand All @@ -81,7 +76,7 @@ describe('Microsoft Graph API', () => {
displayName: 'Updated',
} as GDAPRelationship
jest.spyOn(mockAxios, 'patch').mockResolvedValue({ data: updatedRelationship })
const result = await graphApi.updateGDAPRelationship('1', {
const result = await gdap.updateGDAPRelationship('1', {
displayName: 'Updated',
})
expect(result).toEqual(updatedRelationship)
Expand All @@ -93,7 +88,7 @@ describe('Microsoft Graph API', () => {

it('should delete a GDAP relationship', async () => {
jest.spyOn(mockAxios, 'delete').mockResolvedValue({})
await graphApi.deleteGDAPRelationship('1')
await gdap.deleteGDAPRelationship('1')
expect(mockAxios.delete).toHaveBeenCalledWith(
'/tenantRelationships/delegatedAdminRelationships/1',
)
Expand All @@ -105,7 +100,7 @@ describe('Microsoft Graph API', () => {
const data: CreateGDAPAccessAssignment = {
accessContainer: { id: 'containerId' },
} as never as CreateGDAPAccessAssignment
const result = await graphApi.createGDAPAccessAssignment('relationshipId', data)
const result = await gdap.createGDAPAccessAssignment('relationshipId', data)
expect(result).toEqual(assignment)
expect(mockAxios.post).toHaveBeenCalledWith(
'/tenantRelationships/delegatedAdminRelationships/relationshipId/accessAssignments',
Expand All @@ -119,7 +114,7 @@ describe('Microsoft Graph API', () => {
{ id: '2' },
] as GDAPAccessAssignment[]
jest.spyOn(mockAxios, 'get').mockResolvedValue({ data: { value: assignments } })
const result = await graphApi.getAllGDAPAccessAssignments('relationshipId')
const result = await gdap.getAllGDAPAccessAssignments('relationshipId')
expect(result).toEqual(assignments)
expect(mockAxios.get).toHaveBeenCalledWith(
'/tenantRelationships/delegatedAdminRelationships/relationshipId/accessAssignments',
Expand All @@ -129,7 +124,7 @@ describe('Microsoft Graph API', () => {
it('should get a GDAP access assignment', async () => {
const assignment: GDAPAccessAssignment = { id: '1' } as GDAPAccessAssignment
jest.spyOn(mockAxios, 'get').mockResolvedValue({ data: assignment })
const result = await graphApi.getGDAPAccessAssignment('relationshipId', '1')
const result = await gdap.getGDAPAccessAssignment('relationshipId', '1')
expect(result).toEqual(assignment)
expect(mockAxios.get).toHaveBeenCalledWith(
'/tenantRelationships/delegatedAdminRelationships/relationshipId/accessAssignments/1',
Expand All @@ -145,7 +140,7 @@ describe('Microsoft Graph API', () => {
const data: UpdateGDAPAccessAssignment = {
accessDetails: { unifiedRoles: ['role1'] },
} as never as UpdateGDAPAccessAssignment
const result = await graphApi.updateGDAPAccessAssignment('relationshipId', '1', data)
const result = await gdap.updateGDAPAccessAssignment('relationshipId', '1', data)
expect(result).toEqual(updatedAssignment)
expect(mockAxios.patch).toHaveBeenCalledWith(
'/tenantRelationships/delegatedAdminRelationships/relationshipId/accessAssignments/1',
Expand All @@ -155,7 +150,7 @@ describe('Microsoft Graph API', () => {

it('should delete a GDAP access assignment', async () => {
jest.spyOn(mockAxios, 'delete').mockResolvedValue({})
await graphApi.deleteGDAPAccessAssignment('relationshipId', '1')
await gdap.deleteGDAPAccessAssignment('relationshipId', '1')
expect(mockAxios.delete).toHaveBeenCalledWith(
'/tenantRelationships/delegatedAdminRelationships/relationshipId/accessAssignments/1',
)
Expand Down
Loading