11import { ChatPostMessageResponse } from '@slack/web-api' ;
22import { createStubInstance , SinonStubbedInstance , stub } from 'sinon' ;
33import { assert , mockRequest , mockResponse , runHandler } from '../../../test/utils' ;
4- import { UnprocessableEntityException } from '../../errors' ;
4+ import { UnprocessableEntityException , ConflictException } from '../../errors' ;
55import { AuthToken } from '../../interfaces' ;
66import { AuthTokenRepository } from '../auth-token' ;
77import { MessagesController } from './controller' ;
@@ -20,42 +20,75 @@ describe('MessagesController', () => {
2020 } ) ;
2121
2222 describe ( '#post' , ( ) => {
23- it ( 'throws a UnprocessableEntityException for invalid body' , async ( ) => {
24- oAuthRepository . get . resolves ( { token : 'token' } as AuthToken ) ;
25- messagesRepository . create . resolves ( {
26- result : 'ok' ,
27- } as unknown as ChatPostMessageResponse ) ;
23+ it ( 'throws UnprocessableEntityException for invalid body' , async ( ) => {
2824 const request = mockRequest ( {
2925 body : {
3026 not : 'the' ,
3127 correct : 'body' ,
3228 values : '.' ,
3329 } ,
30+ headers : {
31+ [ 'x-contentful-space-id' ] : 'space123' ,
32+ [ 'x-contentful-environment-id' ] : 'env123' ,
33+ [ 'x-contentful-crn' ] : 'crn:contentful:space:space123' ,
34+ } ,
3435 } ) ;
3536 const next = stub ( ) ;
36- await runHandler ( instance . post ( request , mockResponse ( ) , next ) ) ;
37+ const response = mockResponse ( ) ;
38+
39+ await runHandler ( instance . post ( request , response , next ) ) ;
3740
3841 const error = next . getCall ( 0 ) . args [ 0 ] ;
3942 assert . instanceOf ( error , UnprocessableEntityException ) ;
4043 } ) ;
4144
42- it ( 'returns correct status' , async ( ) => {
43- const result = { ok : true } ;
44- messagesRepository . create . resolves ( result ) ;
45- oAuthRepository . get . resolves ( { token : 'token' } as AuthToken ) ;
45+ it ( 'throws ConflictException when required headers are missing' , async ( ) => {
46+ const request = mockRequest ( {
47+ body : { workspaceId : 'workspace123' , message : 'Hello World!' , channelId : 'channel123' } ,
48+ headers : { } ,
49+ } ) ;
50+ const next = stub ( ) ;
51+ const response = mockResponse ( ) ;
52+
53+ await runHandler ( instance . post ( request , response , next ) ) ;
54+
55+ const error = next . getCall ( 0 ) . args [ 0 ] ;
56+ assert . instanceOf ( error , ConflictException ) ;
57+ assert . include ( error . details ?. errMessage , 'EnvironmentId or spaceId not found in headers' ) ;
58+ } ) ;
59+
60+ it ( 'returns correct response on successful message creation' , async ( ) => {
61+ const slackResponse = {
62+ ok : true ,
63+ channel : 'channel123' ,
64+ ts : '1234567890.123456' ,
65+ message : { text : 'Hello World!' } ,
66+ } as ChatPostMessageResponse ;
67+
68+ oAuthRepository . get . resolves ( { token : 'slack-token-123' } as AuthToken ) ;
69+ messagesRepository . create . resolves ( slackResponse ) ;
4670
4771 const request = mockRequest ( {
48- body : { workspaceId : 'lol ' , message : 'message ' , channelId : 'channel ' } ,
72+ body : { workspaceId : 'workspace123 ' , message : 'Hello World! ' , channelId : 'channel123 ' } ,
4973 headers : {
50- [ 'x-contentful-space-id' ] : 'space' ,
51- [ 'x-contentful-environment-id' ] : 'env' ,
74+ [ 'x-contentful-space-id' ] : 'space123' ,
75+ [ 'x-contentful-environment-id' ] : 'env123' ,
76+ [ 'x-contentful-crn' ] : 'crn:contentful:space:space123' ,
5277 } ,
5378 } ) ;
5479 const next = stub ( ) ;
55- const response = mockResponse ( ) ;
80+ const statusStub = stub ( ) . returnsThis ( ) ;
81+ const jsonStub = stub ( ) . returnsThis ( ) ;
82+ const response = mockResponse ( {
83+ status : statusStub ,
84+ json : jsonStub ,
85+ } ) ;
86+
5687 await runHandler ( instance . post ( request , response , next ) ) ;
5788
58- assert . calledWith ( response . sendStatus , 204 ) ;
89+ assert . calledWith ( statusStub , 200 ) ;
90+ assert . calledWith ( jsonStub , slackResponse ) ;
91+ assert . notCalled ( next ) ;
5992 } ) ;
6093 } ) ;
6194} ) ;
0 commit comments