1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-14 10:26:19 +01:00
bitwarden-browser/libs/common/spec/services/organization-domain/org-domain-api.service.spec.ts
Jared Snider 36633bcb04
[SG-147] - Feature/web-organization-domain-claiming (#4734)
* Feature/SG-680 - Create Domain Verification Comp (#4283)

* domain-base.ts - added link to Architectural docs describing domain base class purpose

* SG-680 - (1) Created Org Domain API and observable based data store service (2) Created required response and request models

* SG-680 - Renaming org domain service abstractions to match existing convention

* SG-680 - (1) Updated getByOrgId method to return array of data to match back end + renamed it as such (2) Updated OrgDomainApiService get methods to update the OrgDomainService observables

* Two-factor-setup comp - change "tabbed-header" class to "page-header" to achieve visual consistency with other settings components.

* SG-680 - Refactor Org Domain API & domain services to conform to ADR 0013 - Avoid layered folder structure for request/response models (i.e., put models near "owner" services)

* SG-680 - Update Organization model to include a canManageDomainVerification check

* SG-680 - Created Domain Verification component and started scaffolding out HTML

* SG-680 - New OrgDomain state and API services need to be registered on jslib-services.module in order to be injectable into components for use (this is what maps abstractions to implementation for dependency injection)

* SG-680 - OrgDomainApiServiceAbstraction should be an abstract class

* SG-680 - Update OrgDomainApiService to use ListResponse and map into OrganizationDomainResponse properly

* SG-680 - Moved domain verification comp into subfolder to add clarity in folder structure

* SG-680 - Good start on Domain Add Edit Dialog

* SG-680 - Domain Add Edit Dialog - (1) Random generation of DNS TXT Record now working (2) DNS TXT Record Copyable (3) Additional translations added (4) Info callout added

* SG-680 - Domain Add Edit Dialog - (1) Added custom validator for domain name (2) Disable verify btn if form invalid

* SG-680 - Updated Domain Name custom validator to pass back error message in format the error.component.ts expected so it can be displayed without an untranslated error prefix of "invalidDomainName"

* Form-button - Added useful note regarding use of the bitFormButton directive and how it requires the use of the bitButton directive as well.

* SG-680 - OrgDomain.service - replace delete with splice as delete doesn't actually alter array. Duh.

* SG-680 -  Domain verification progress - (1) Table layout + loading working for the most part (more translations needed (2) Add & edit opening dialog (3) Dialog first draft of save and verify

* SG-680 - DomainAddEditDialog - Unique domain name enforcement implemented

* SG-680 - Domain Name Custom Reactive forms validator refactor - swapped to regex to support proper domain format (which now enforces the requirement of a .com or similar)

* SG-680 - OrgDomainApi svc - must await send of delete call otherwise runs synchronously. Duh.

* SG-680 - Domain verification progress - (1) CopyDnsTxt added to state service (2) Refactored dialog to use async actions (3) Dialog form changes now mark form controls as touched for more responsive error handling

* SG-680 - Domain-add-edit-dialog - Confirmation required now for domain deletion

* SG-680 - Domain verification table options now supports removing domains with confirmation prompt

* Shared module - merge conflict resolution + removing unused imports so I can check this in.

* SG-680 - Adding missing translations

* SG-680 - Comment clean up + todo

* Revert "Shared module - merge conflict resolution + removing unused imports so I can check this in."

This reverts commit 98fe346e67.

* SG-680 - DomainAddEditDialog - Replace bitAction with leveraging bitSubmit so that when users hit enter in a field the form gets submitted.

* SG-680 - Added httpStatusCode enum

* SG-680 - OrgDomainAPI - Verify endpoint now returns domain response model so upsert to sync obs state service

* SG-680 - Domain Verification comp - (1) Display last checked date (2) Verify first attempt in place (3) justify options content per design

* SG-680 - DomainAddEditDialog - Validation and error handling overhaul

* SG-680 - DomainAddEditDialog - (1) Autofocus domain name on new domain creation (2) Removed form.invalid == form disabled logic because of accessibility concerns

* SG-680 - OrgDomainResponse model refactor - back end is sending lower cased props

* SG-680 - OrgDomain service refactor - (1) Use proper abstraction for i18n svc (2) Don't make non-async methods async for no reason

* SG-680 - OrgDomainService - Added test suite

* SG-680 - Renaming httpStatusCode.enum to strip off .enum in attempt to pass eslint issue

* SG-680 - Renaming httpStatusCode enum file again to remove all capitalized letters to pass eslint rules.

* SG-680 - Updating HttpStatusCode import b/c auto import update missed it.

* SG-680 - DomainAddEditDialog - Don't show callout if domain is verified

* SG-680 - DomainVerificationComp - Add error handling to verify to handle case where domain isn't available

* SG-680 - OrgDomainApiSvc - svc should use abstractions in constructor

* SG-680 - OrgDomainApiSvc - added full test suite

* SG-680 - OrgDomainSvc test suite - fixing broken test

* SG-680 - Domain Verification Validation Scenario: show form control error when domain verification fails  - (1) Enhanced bitSubmit to optionally allowDisabledFormSubmit (2) Enhanced bitInput to optionally allow showErrorsWhenDisabled + added new docs

* SG-680 - Adjusting location & name of the bitInput docs to be in the top level docs section to match historical consensus and existing pattern.

* SG-680 - Removed TODO for adding tests since I've already added tests.

* SG-680 - DomainAddEditDialog - Handle verify domain conflict exceptions just in case

* SG-680 - Adjusting location of Domain verification settings item in the organization settings menu to match figma.

* SG-680 - Removing unnecessary comment

* SG-680 - Domain Verification component - updated svg to not have alt text so it is treated as decorative by screen readers for accessibility.

* SG-680 - Fixing messages.json missing }

* SG-680 - DomainAddEditDialog - Hardcoding inputs to bit-dialog as component vars are not needed for dynamic anything right now.

* SG-680 - Dialog comment refactor

* SG-680 -  OrgDomainSvc - comment and console log removals.

* SG-680 - Updating OrgDomain Service test suites to have better test titles.

* Defect/web org domain claiming bugfixes (#4458)

* SG-949 - OrgDomainVerification - Domain name validator now supports n levels of subdomains as well as top level domains.

* SG-955 - On domain verification error or failure, call to update the individual org domain item to get an updated last checked date on the client.

* SG-953 - In domain verification dialog edit, if verify called and failed, then must manually mark domain name as touched for errors to show up.

* SG-954 - Domain Verification edit dialog - Fixing delete button not having trash icon displayed + added i18n translation for title prop.

* SG-956 - Fixing domain claiming event logs so that they show up on the client (more to do as there are some events missing client & member)

* Form button directive comment update

* SG-977 - Event Log improvements: (1) Add new device type of server (2) Add EventSystemUser mapping to translated value.  The end result is that both SCIM and Domain verification logs properly show server as the client and SCIM or Domain verification as the member.

* Add comment to clarify use of SCIM in EVENT_SYSTEM_USER_TO_TRANSLATION  dict

* DeviceType.Server must be incremented to 22 b/c server master already has a DeviceType.SDK of 21.

* Add SDK w/ value of 21 to DeviceType to match master server

* Defect/web org domain claiming bugfixes the sequel (#4530)

* Update <bit-table> to latest standard so it works again (swap body from ng-container to ng-template)

* Input directive - showErrorsWhenDisabled case doesn't need to care if input isActive or not.

* SG-949 - Update domain name validator regex to prevent http://, https://, and www.

* SG-771 - Added claimed domain logic to web client (#4603)

* SG-771 / SG-772 / SG-743 - Add claimed domain logic which skips entry of Org SSO Id when an org has a claimed and verified domain to web, browser, and desktop.

* Fix lint errors by adding button types

---------

Co-authored-by: SmithThe4th <gsmith@bitwarden.com>
2023-02-15 15:50:39 -05:00

174 lines
6.5 KiB
TypeScript

import { mock, mockReset } from "jest-mock-extended";
import { lastValueFrom } from "rxjs";
import { ApiService } from "@bitwarden/common/abstractions/api.service";
import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
import { OrganizationDomainResponse } from "@bitwarden/common/abstractions/organization-domain/responses/organization-domain.response";
import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUtils.service";
import { OrgDomainApiService } from "@bitwarden/common/services/organization-domain/org-domain-api.service";
import { OrgDomainService } from "@bitwarden/common/services/organization-domain/org-domain.service";
const mockedGetAllByOrgIdResponse: any = {
data: [
{
id: "ca01a674-7f2f-45f2-8245-af6d016416b7",
organizationId: "cb903acf-2361-4072-ae32-af6c014943b6",
txt: "bw=EUX6UKR8A68igAJkmodwkzMiqB00u7Iyq1QqALu6jFID",
domainName: "test.com",
creationDate: "2022-12-16T21:36:28.68Z",
nextRunDate: "2022-12-17T09:36:28.68Z",
jobRunCount: 0,
verifiedDate: null as any,
lastCheckedDate: "2022-12-16T21:36:28.7633333Z",
object: "organizationDomain",
},
{
id: "adbd44c5-90d5-4537-97e6-af6d01644870",
organizationId: "cb903acf-2361-4072-ae32-af6c014943b6",
txt: "bw=Ql4fCfDacmcjwyAP9BPmvhSMTCz4PkEDm4uQ3fH01pD4",
domainName: "test2.com",
creationDate: "2022-12-16T21:37:10.9566667Z",
nextRunDate: "2022-12-17T09:37:10.9566667Z",
jobRunCount: 0,
verifiedDate: "totally verified",
lastCheckedDate: "2022-12-16T21:37:11.1933333Z",
object: "organizationDomain",
},
{
id: "05cf3ab8-bcfe-4b95-92e8-af6d01680942",
organizationId: "cb903acf-2361-4072-ae32-af6c014943b6",
txt: "bw=EQNUs77BWQHbfSiyc/9nT3wCen9z2yMn/ABCz0cNKaTx",
domainName: "test3.com",
creationDate: "2022-12-16T21:50:50.96Z",
nextRunDate: "2022-12-17T09:50:50.96Z",
jobRunCount: 0,
verifiedDate: null,
lastCheckedDate: "2022-12-16T21:50:51.0933333Z",
object: "organizationDomain",
},
],
continuationToken: null as any,
object: "list",
};
const mockedOrgDomainServerResponse = {
id: "ca01a674-7f2f-45f2-8245-af6d016416b7",
organizationId: "cb903acf-2361-4072-ae32-af6c014943b6",
txt: "bw=EUX6UKR8A68igAJkmodwkzMiqB00u7Iyq1QqALu6jFID",
domainName: "test.com",
creationDate: "2022-12-16T21:36:28.68Z",
nextRunDate: "2022-12-17T09:36:28.68Z",
jobRunCount: 0,
verifiedDate: null as any,
lastCheckedDate: "2022-12-16T21:36:28.7633333Z",
object: "organizationDomain",
};
const mockedOrgDomainResponse = new OrganizationDomainResponse(mockedOrgDomainServerResponse);
describe("Org Domain API Service", () => {
let orgDomainApiService: OrgDomainApiService;
const apiService = mock<ApiService>();
let orgDomainService: OrgDomainService;
const platformUtilService = mock<PlatformUtilsService>();
const i18nService = mock<I18nService>();
beforeEach(() => {
orgDomainService = new OrgDomainService(platformUtilService, i18nService);
mockReset(apiService);
orgDomainApiService = new OrgDomainApiService(orgDomainService, apiService);
});
it("instantiates", () => {
expect(orgDomainApiService).not.toBeFalsy();
});
it("getAllByOrgId retrieves all org domains and calls orgDomainSvc replace", () => {
apiService.send.mockResolvedValue(mockedGetAllByOrgIdResponse);
expect(lastValueFrom(orgDomainService.orgDomains$)).resolves.toHaveLength(0);
const orgDomainSvcReplaceSpy = jest.spyOn(orgDomainService, "replace");
orgDomainApiService
.getAllByOrgId("fakeOrgId")
.then((orgDomainResponses: Array<OrganizationDomainResponse>) => {
expect(orgDomainResponses).toHaveLength(3);
expect(orgDomainSvcReplaceSpy).toHaveBeenCalled();
expect(lastValueFrom(orgDomainService.orgDomains$)).resolves.toHaveLength(3);
});
});
it("getByOrgIdAndOrgDomainId retrieves single org domain and calls orgDomainSvc upsert", () => {
apiService.send.mockResolvedValue(mockedOrgDomainServerResponse);
expect(lastValueFrom(orgDomainService.orgDomains$)).resolves.toHaveLength(0);
const orgDomainSvcUpsertSpy = jest.spyOn(orgDomainService, "upsert");
orgDomainApiService
.getByOrgIdAndOrgDomainId("fakeOrgId", "fakeDomainId")
.then((orgDomain: OrganizationDomainResponse) => {
expect(orgDomain.id).toEqual(mockedOrgDomainServerResponse.id);
expect(orgDomainSvcUpsertSpy).toHaveBeenCalled();
expect(lastValueFrom(orgDomainService.orgDomains$)).resolves.toHaveLength(1);
});
});
it("post success should call orgDomainSvc upsert", () => {
apiService.send.mockResolvedValue(mockedOrgDomainServerResponse);
expect(lastValueFrom(orgDomainService.orgDomains$)).resolves.toHaveLength(0);
const orgDomainSvcUpsertSpy = jest.spyOn(orgDomainService, "upsert");
orgDomainApiService
.post("fakeOrgId", mockedOrgDomainResponse)
.then((orgDomain: OrganizationDomainResponse) => {
expect(orgDomain.id).toEqual(mockedOrgDomainServerResponse.id);
expect(orgDomainSvcUpsertSpy).toHaveBeenCalled();
expect(lastValueFrom(orgDomainService.orgDomains$)).resolves.toHaveLength(1);
});
});
it("verify success should call orgDomainSvc upsert", () => {
apiService.send.mockResolvedValue(mockedOrgDomainServerResponse);
expect(lastValueFrom(orgDomainService.orgDomains$)).resolves.toHaveLength(0);
const orgDomainSvcUpsertSpy = jest.spyOn(orgDomainService, "upsert");
orgDomainApiService
.verify("fakeOrgId", "fakeOrgId")
.then((orgDomain: OrganizationDomainResponse) => {
expect(orgDomain.id).toEqual(mockedOrgDomainServerResponse.id);
expect(orgDomainSvcUpsertSpy).toHaveBeenCalled();
expect(lastValueFrom(orgDomainService.orgDomains$)).resolves.toHaveLength(1);
});
});
it("delete success should call orgDomainSvc delete", () => {
apiService.send.mockResolvedValue(true);
orgDomainService.upsert([mockedOrgDomainResponse]);
expect(lastValueFrom(orgDomainService.orgDomains$)).resolves.toHaveLength(1);
const orgDomainSvcDeleteSpy = jest.spyOn(orgDomainService, "delete");
orgDomainApiService.delete("fakeOrgId", "fakeOrgId").then(() => {
expect(orgDomainSvcDeleteSpy).toHaveBeenCalled();
expect(lastValueFrom(orgDomainService.orgDomains$)).resolves.toHaveLength(0);
});
});
// TODO: add Get Domain SSO method: Retrieves SSO provider information given a domain name
// when added on back end
});