1+ import { AuthenticateApp , AuthenticateAppInputs } from "../AuthenticateApp" ;
2+
3+ // Restore global fetch
4+ const originalFetch = global . fetch ;
5+ afterEach ( ( ) => {
6+ global . fetch = originalFetch ;
7+ } ) ;
8+
9+ const mockFetch = jest . fn ( ) ;
10+
11+ function mockResponseOnce (
12+ response : Record < string , unknown > ,
13+ callback ?: ( input : RequestInfo , init : RequestInit ) => void
14+ ) {
15+ global . fetch = mockFetch ;
16+ mockFetch . mockImplementationOnce ( ( input , init ) => {
17+ callback ?.( input , init ) ;
18+ return Promise . resolve ( {
19+ ok : true ,
20+ json : ( ) => Promise . resolve ( response ) ,
21+ status : 200 ,
22+ statusText : "OK" ,
23+ } ) ;
24+ } ) ;
25+ }
26+
27+ describe ( "AuthenticateApp" , ( ) => {
28+ describe ( "execute" , ( ) => {
29+ it ( "requires url input" , async ( ) => {
30+ const activity = new AuthenticateApp ( ) ;
31+ const inputs : AuthenticateAppInputs = {
32+ url : "" ,
33+ companyAccount : "c" ,
34+ appName : "a" ,
35+ secret : "s"
36+ } ;
37+
38+ await expect ( ( ) => activity . execute ( inputs ) ) . rejects . toThrow ( "url is required" ) ;
39+ } ) ;
40+ it ( "requires companyAccount input" , async ( ) => {
41+ const activity = new AuthenticateApp ( ) ;
42+ const inputs : AuthenticateAppInputs = {
43+ url : "https://test" ,
44+ companyAccount : "" ,
45+ appName : "a" ,
46+ secret : "s"
47+ } ;
48+
49+ await expect ( ( ) => activity . execute ( inputs ) ) . rejects . toThrow ( "companyAccount is required" ) ;
50+ } ) ;
51+ it ( "requires appName input" , async ( ) => {
52+ const activity = new AuthenticateApp ( ) ;
53+ const inputs : AuthenticateAppInputs = {
54+ url : "https://test" ,
55+ companyAccount : "a" ,
56+ appName : "" ,
57+ secret : "s"
58+ } ;
59+
60+ await expect ( ( ) => activity . execute ( inputs ) ) . rejects . toThrow ( "appName is required" ) ;
61+ } ) ;
62+ it ( "requires secret input" , async ( ) => {
63+ const activity = new AuthenticateApp ( ) ;
64+ const inputs : AuthenticateAppInputs = {
65+ url : "https://test" ,
66+ companyAccount : "c" ,
67+ appName : "a" ,
68+ secret : ""
69+ } ;
70+
71+ await expect ( ( ) => activity . execute ( inputs ) ) . rejects . toThrow ( "secret is required" ) ;
72+ } ) ;
73+ it ( "calls the API using POST" , async ( ) => {
74+ const activity = new AuthenticateApp ( ) ;
75+ const inputs : AuthenticateAppInputs = {
76+ url : "https://test" ,
77+ companyAccount : "c" ,
78+ appName : "a" ,
79+ secret : "s"
80+ } ;
81+ const authResponse = { foo : "bar" } ;
82+
83+ mockResponseOnce ( authResponse , ( input , init ) => {
84+ expect ( input ) . toBe ( `https://test/api/token/app` ) ;
85+ expect ( init . method ) . toBe ( "post" ) ;
86+ expect ( init . headers ?. [ "content-type" ] ) . toBe ( "application/json" ) ;
87+ if ( typeof init . body === "string" ) {
88+ expect ( JSON . parse ( init . body ) ) . toStrictEqual ( {
89+ appName : "a" ,
90+ companyAccount : "c" ,
91+ secret : "s"
92+ } ) ;
93+ } else {
94+ fail ( "body was not a string" )
95+ }
96+ } ) ;
97+
98+ const result = await activity . execute ( inputs ) ;
99+ expect ( result ) . toStrictEqual ( {
100+ service : {
101+ url : inputs . url ,
102+ ...authResponse ,
103+ }
104+ } )
105+ } ) ;
106+ } ) ;
107+ } ) ;
0 commit comments