diff --git a/.gitignore b/.gitignore index 0afd1ed..0f7f4c2 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,6 @@ package-lock.json pnpm-lock.yaml src/auth/tickets -tests/mocks/credentials +*/mocks/credentials docs/.vitepress/dist \ No newline at end of file diff --git a/src/auth/afip-auth.ts b/src/auth/afip-auth.ts index b9ca9a7..dbc0c91 100644 --- a/src/auth/afip-auth.ts +++ b/src/auth/afip-auth.ts @@ -196,7 +196,12 @@ export class AfipAuth { throw new Error(`Access denied to ticket file: ${filePath}`); } - const fileData = await fs.readFile(filePath, "utf8"); + let fileData; + try { + fileData = await fs.readFile(filePath, "utf8"); + } catch (error) { + return undefined; + } try { return new AccessTicket(JSON.parse(fileData)); diff --git a/src/services/afip.service.ts b/src/services/afip.service.ts index 22fbbdc..e6cb7a1 100644 --- a/src/services/afip.service.ts +++ b/src/services/afip.service.ts @@ -111,7 +111,7 @@ export class AfipService { throw new Error("Credentials expired."); } } else if (!this._credentials || this._credentials.isExpired()) { - this._credentials = await this._afipAuth.login(this._serviceName); + this._credentials = await this.login(); } return this._credentials.getWSAuthFormat(this.context.cuit); diff --git a/tests/auth/access-ticket.test.ts b/tests/unit/auth/access-ticket.test.ts similarity index 92% rename from tests/auth/access-ticket.test.ts rename to tests/unit/auth/access-ticket.test.ts index b6d6c42..da62bb0 100644 --- a/tests/auth/access-ticket.test.ts +++ b/tests/unit/auth/access-ticket.test.ts @@ -1,8 +1,8 @@ -import { mockLoginCredentials } from "./../mocks/data/credential-json.mock"; +import { mockLoginCredentials } from "../../mocks/data/credential-json.mock"; import moment from "moment"; -import { AccessTicket } from "../../src/auth/access-ticket"; -import { ILoginCredentials } from "../../src/types"; -import EnvTest from "../utils/env-test"; +import { AccessTicket } from "../../../src/auth/access-ticket"; +import { ILoginCredentials } from "../../../src/types"; +import EnvTest from "../../utils/env-test"; describe("Access Ticket", () => { describe("getSign", () => { diff --git a/tests/auth/afip-auth.test.ts b/tests/unit/auth/afip-auth.test.ts similarity index 85% rename from tests/auth/afip-auth.test.ts rename to tests/unit/auth/afip-auth.test.ts index 34d0201..ab75ef5 100644 --- a/tests/auth/afip-auth.test.ts +++ b/tests/unit/auth/afip-auth.test.ts @@ -1,12 +1,12 @@ -import { mockLoginCredentials } from "./../mocks/data/credential-json.mock"; -import { AfipAuth } from "./../../src/auth/afip-auth"; -import { ServiceNamesEnum } from "../../src/soap/service-names.enum"; -import { Context, ILoginCredentials } from "./../../src/types"; -import { AccessTicket } from "../../src/auth/access-ticket"; +import { mockLoginCredentials } from "../../mocks/data/credential-json.mock"; +import { AfipAuth } from "../../../src/auth/afip-auth"; +import { ServiceNamesEnum } from "../../../src/soap/service-names.enum"; +import { Context, ILoginCredentials } from "../../../src/types"; +import { AccessTicket } from "../../../src/auth/access-ticket"; import moment from "moment"; import { promises as fs } from "fs"; -jest.mock("../../src/soap/soap-client-facade", () => ({ +jest.mock("../../../src/soap/soap-client-facade", () => ({ create: jest.fn(() => ({ loginCmsAsync: jest.fn(() => { return [{ loginCmsReturn: "" }, "", {}, ""]; @@ -14,12 +14,12 @@ jest.mock("../../src/soap/soap-client-facade", () => ({ })), })); -jest.mock("../../src/utils/parser", () => ({ +jest.mock("../../../src/utils/parser", () => ({ jsonToXml: jest.fn((json) => JSON.stringify(json)), xmlToJson: jest.fn((xml) => JSON.parse(xml)), })); -jest.mock("../../src/utils/crypt-data", () => ({ +jest.mock("../../../src/utils/crypt-data", () => ({ Cryptography: jest.fn(() => ({ sign: jest.fn(() => "signedTRA"), })), @@ -38,7 +38,7 @@ jest.mock("fs", () => ({ }, })); -jest.mock("../../src/utils/logger", () => ({ +jest.mock("../../../src/utils/logger", () => ({ error: jest.fn(), })); diff --git a/tests/services/afip.service.test.ts b/tests/unit/services/afip.service.test.ts similarity index 83% rename from tests/services/afip.service.test.ts rename to tests/unit/services/afip.service.test.ts index 0df9e96..b76d2b9 100644 --- a/tests/services/afip.service.test.ts +++ b/tests/unit/services/afip.service.test.ts @@ -1,14 +1,14 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import { Client } from "soap"; -import { EndpointsEnum, SoapServiceVersion } from "../../src/enums"; -import { AfipService } from "../../src/services/afip.service"; -import { IServiceSoap12Soap } from "../../src/soap/interfaces/Service/ServiceSoap12"; -import { ServiceNamesEnum } from "../../src/soap/service-names.enum"; -import { WsdlPathEnum } from "../../src/soap/wsdl-path.enum"; -import { AfipServiceSoapParam, Context } from "../../src/types"; -import { testCuit } from "../mocks/data/voucher.mock"; -import { TestConfigUtils } from "../utils/config.utils"; -import { mockFn } from "../utils/jest.utils"; +import { EndpointsEnum, SoapServiceVersion } from "../../../src/enums"; +import { AfipService } from "../../../src/services/afip.service"; +import { IServiceSoap12Soap } from "../../../src/soap/interfaces/Service/ServiceSoap12"; +import { ServiceNamesEnum } from "../../../src/soap/service-names.enum"; +import { WsdlPathEnum } from "../../../src/soap/wsdl-path.enum"; +import { AfipServiceSoapParam, Context } from "../../../src/types"; +import { testCuit } from "../../mocks/data/voucher.mock"; +import { TestConfigUtils } from "../../utils/config.utils"; +import { mockFn } from "../../utils/jest.utils"; describe("AfipService", () => { let afipService: AfipService; diff --git a/tests/services/electronic-billings.service.test.ts b/tests/unit/services/electronic-billings.service.test.ts similarity index 96% rename from tests/services/electronic-billings.service.test.ts rename to tests/unit/services/electronic-billings.service.test.ts index fb8758e..3cbfa2c 100644 --- a/tests/services/electronic-billings.service.test.ts +++ b/tests/unit/services/electronic-billings.service.test.ts @@ -13,18 +13,18 @@ import { FEParamGetTiposMonedasAsyncReturnMocks, FEParamGetTiposOpcionalAsyncReturnMocks, FEParamGetTiposTributosAsyncReturnMocks, -} from "../mocks/data/soapClient.mock"; +} from "../../mocks/data/soapClient.mock"; import { data, testCbteNro, testCbteTipo, testCuit, testPtoVta, -} from "../mocks/data/voucher.mock"; -import { Afip } from "../../src/afip"; -import { TestConfigUtils } from "../utils/config.utils"; -import { AccessTicket } from "../../src/auth/access-ticket"; -import { mockLoginCredentials } from "../mocks/data/credential-json.mock"; +} from "../../mocks/data/voucher.mock"; +import { Afip } from "../../../src/afip"; +import { TestConfigUtils } from "../../utils/config.utils"; +import { AccessTicket } from "../../../src/auth/access-ticket"; +import { mockLoginCredentials } from "../../mocks/data/credential-json.mock"; describe("Electronic Billings Service", () => { let afip: Afip; diff --git a/tests/services/register-inscription-proof.service.test.ts b/tests/unit/services/register-inscription-proof.service.test.ts similarity index 85% rename from tests/services/register-inscription-proof.service.test.ts rename to tests/unit/services/register-inscription-proof.service.test.ts index b05c7ce..bf5933f 100644 --- a/tests/services/register-inscription-proof.service.test.ts +++ b/tests/unit/services/register-inscription-proof.service.test.ts @@ -1,13 +1,13 @@ -import { testCuit } from "./../mocks/data/voucher.mock"; -import { Afip } from "../../src/afip"; -import { TestConfigUtils } from "../utils/config.utils"; +import { testCuit } from "../../mocks/data/voucher.mock"; +import { Afip } from "../../../src/afip"; +import { TestConfigUtils } from "../../utils/config.utils"; import { dummyAsyncReturnMocks, getPersonaList_v2AsyncReturnMocks, getPersona_v2AsyncReturnMocks, -} from "../mocks/data/soapClient.mock"; -import { mockLoginCredentials } from "../mocks/data/credential-json.mock"; -import { RegisterInscriptionProofService } from "../../src/services/register-inscription-proof.service"; +} from "../../mocks/data/soapClient.mock"; +import { mockLoginCredentials } from "../../mocks/data/credential-json.mock"; +import { RegisterInscriptionProofService } from "../../../src/services/register-inscription-proof.service"; describe("Register Inscription Proof Service", () => { const originalNodeTlsRejectUnauthStatus = diff --git a/tests/services/register-scope-five.service.test.ts b/tests/unit/services/register-scope-five.service.test.ts similarity index 85% rename from tests/services/register-scope-five.service.test.ts rename to tests/unit/services/register-scope-five.service.test.ts index 2253cff..e2ed50d 100644 --- a/tests/services/register-scope-five.service.test.ts +++ b/tests/unit/services/register-scope-five.service.test.ts @@ -1,13 +1,13 @@ -import { testCuit } from "./../mocks/data/voucher.mock"; -import { Afip } from "../../src/afip"; -import { TestConfigUtils } from "../utils/config.utils"; +import { testCuit } from "../../mocks/data/voucher.mock"; +import { Afip } from "../../../src/afip"; +import { TestConfigUtils } from "../../utils/config.utils"; import { dummyAsyncReturnMocks, getPersonaList_v2AsyncReturnMocks, getPersona_v2AsyncReturnMocks, -} from "../mocks/data/soapClient.mock"; -import { mockLoginCredentials } from "../mocks/data/credential-json.mock"; -import { RegisterScopeFiveService } from "../../src/services/register-scope-five.service"; +} from "../../mocks/data/soapClient.mock"; +import { mockLoginCredentials } from "../../mocks/data/credential-json.mock"; +import { RegisterScopeFiveService } from "../../../src/services/register-scope-five.service"; describe("Register Scope Five Service", () => { const originalNodeTlsRejectUnauthStatus = diff --git a/tests/services/register-scope-four.service.test.ts b/tests/unit/services/register-scope-four.service.test.ts similarity index 82% rename from tests/services/register-scope-four.service.test.ts rename to tests/unit/services/register-scope-four.service.test.ts index b41c351..5b7c86e 100644 --- a/tests/services/register-scope-four.service.test.ts +++ b/tests/unit/services/register-scope-four.service.test.ts @@ -1,12 +1,12 @@ -import { testCuit } from "../mocks/data/voucher.mock"; -import { Afip } from "../../src/afip"; -import { TestConfigUtils } from "../utils/config.utils"; +import { testCuit } from "../../mocks/data/voucher.mock"; +import { Afip } from "../../../src/afip"; +import { TestConfigUtils } from "../../utils/config.utils"; import { scopeFourDummyAsyncReturnMocks, scopeFourGetPersonaAsyncReturnMocks, -} from "../mocks/data/soapClient.mock"; -import { mockLoginCredentials } from "../mocks/data/credential-json.mock"; -import { RegisterScopeFourService } from "../../src/services/register-scope-four.service"; +} from "../../mocks/data/soapClient.mock"; +import { mockLoginCredentials } from "../../mocks/data/credential-json.mock"; +import { RegisterScopeFourService } from "../../../src/services/register-scope-four.service"; describe("Register Scope Four Service", () => { const originalNodeTlsRejectUnauthStatus = diff --git a/tests/services/register-scope-ten.service.test.ts b/tests/unit/services/register-scope-ten.service.test.ts similarity index 82% rename from tests/services/register-scope-ten.service.test.ts rename to tests/unit/services/register-scope-ten.service.test.ts index dccb212..cdff6c5 100644 --- a/tests/services/register-scope-ten.service.test.ts +++ b/tests/unit/services/register-scope-ten.service.test.ts @@ -1,12 +1,12 @@ -import { testCuit } from "../mocks/data/voucher.mock"; -import { Afip } from "../../src/afip"; -import { TestConfigUtils } from "../utils/config.utils"; -import { mockLoginCredentials } from "../mocks/data/credential-json.mock"; -import { RegisterScopeTenService } from "../../src/services/register-scope-ten.service"; +import { testCuit } from "../../mocks/data/voucher.mock"; +import { Afip } from "../../../src/afip"; +import { TestConfigUtils } from "../../utils/config.utils"; +import { mockLoginCredentials } from "../../mocks/data/credential-json.mock"; +import { RegisterScopeTenService } from "../../../src/services/register-scope-ten.service"; import { scopeTenDummyAsyncReturnMocks, scopeTenGetPersonaAsyncReturnMocks, -} from "../mocks/data/soapClient.mock"; +} from "../../mocks/data/soapClient.mock"; describe("Register Scope Ten Service", () => { const originalNodeTlsRejectUnauthStatus = diff --git a/tests/services/register-scope-thirteen.service.test.ts b/tests/unit/services/register-scope-thirteen.service.test.ts similarity index 82% rename from tests/services/register-scope-thirteen.service.test.ts rename to tests/unit/services/register-scope-thirteen.service.test.ts index 619fb2d..a2c2282 100644 --- a/tests/services/register-scope-thirteen.service.test.ts +++ b/tests/unit/services/register-scope-thirteen.service.test.ts @@ -1,12 +1,12 @@ -import { testCuit } from "../mocks/data/voucher.mock"; -import { Afip } from "../../src/afip"; -import { TestConfigUtils } from "../utils/config.utils"; -import { mockLoginCredentials } from "../mocks/data/credential-json.mock"; -import { RegisterScopeThirteenService } from "../../src/services/register-scope-thirteen.service"; +import { testCuit } from "../../mocks/data/voucher.mock"; +import { Afip } from "../../../src/afip"; +import { TestConfigUtils } from "../../utils/config.utils"; +import { mockLoginCredentials } from "../../mocks/data/credential-json.mock"; +import { RegisterScopeThirteenService } from "../../../src/services/register-scope-thirteen.service"; import { scopeThirteenDummyAsyncReturnMocks, scopeThirteenGetPersonaAsyncReturnMocks, -} from "../mocks/data/soapClient.mock"; +} from "../../mocks/data/soapClient.mock"; describe("Register Scope Thirteen Service", () => { const originalNodeTlsRejectUnauthStatus =