|
| 1 | +import { |
| 2 | + BaseMessage, |
| 3 | + isMessage, |
| 4 | + NotificationMessage, |
| 5 | + SubscriptionConfirmationMessage, |
| 6 | + UnsubscribeConfirmationMessage, |
| 7 | +} from "../lib/Message"; |
| 8 | + |
| 9 | +const validNotification: NotificationMessage = { |
| 10 | + Type: "Notification", |
| 11 | + MessageId: "da41e39f-ea4d-435a-b922-c6aae3915ebe", |
| 12 | + TopicArn: "arn:aws:sns:us-west-2:123456789012:MyTopic", |
| 13 | + Subject: "test", |
| 14 | + Message: "test message", |
| 15 | + Timestamp: "2012-04-25T21:49:25.719Z", |
| 16 | + SignatureVersion: "1", |
| 17 | + Signature: "EXAMPLElDMXvB8r9R83tGoNn0ecwd5UjllzsvSvbItzfaMpN2nk5HVSw7XnOn/49IkxDKz8YrlH2qJXj2iZB0Zo2O71c4qQk1fMUDi3LGpij7RCW7AW9vYYsSqIKRnFS94ilu7NFhUzLiieYr4BKHpdTmdD6c0esKEYBpabxDSc=", |
| 18 | + SigningCertURL: "https://sns.us-west-2.amazonaws.com/SimpleNotificationService-f3ecfb7224c7233fe7bb5f59f96de52f.pem", |
| 19 | + UnsubscribeURL: "https://sns.us-west-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-west-2:123456789012:MyTopic:2bcfbf39-05c3-41de-beaa-fcfcc21c8f55" |
| 20 | +}; |
| 21 | + |
| 22 | +const validSubscriptionConfirmation: SubscriptionConfirmationMessage = { |
| 23 | + Type: "SubscriptionConfirmation", |
| 24 | + MessageId: "165545c9-2a5c-472c-8df2-7ff2be2b3b1b", |
| 25 | + Token: "2336412f37fb687f5d51e6e241d09c805a5a57b30d712f794cc5f6a988666d92768dd60a747ba6f3beb71854e285d6ad02428b09ceece29417f1f02d609c582afbacc99c583a916b9981dd2728f4ae6fdb82efd087cc3b7849e05798d2d2785c03b0879594eeac82c01f235d0e717736", |
| 26 | + TopicArn: "arn:aws:sns:us-west-2:123456789012:MyTopic", |
| 27 | + Message: "You have chosen to subscribe to the topic arn:aws:sns:us-west-2:123456789012:MyTopic.\nTo confirm the subscription, visit the SubscribeURL included in this message.", |
| 28 | + SubscribeURL: "https://sns.us-west-2.amazonaws.com/?Action=ConfirmSubscription&TopicArn=arn:aws:sns:us-west-2:123456789012:MyTopic&Token=2336412f37fb687f5d51e6e241d09c805a5a57b30d712f794cc5f6a988666d92768dd60a747ba6f3beb71854e285d6ad02428b09ceece29417f1f02d609c582afbacc99c583a916b9981dd2728f4ae6fdb82efd087cc3b7849e05798d2d2785c03b0879594eeac82c01f235d0e717736", |
| 29 | + Timestamp: "2012-04-26T20:45:04.751Z", |
| 30 | + SignatureVersion: "1", |
| 31 | + Signature: "EXAMPLEpH+DcEwjAPg8O9mY8dReBSwksfg2S7WKQcikcNKWLQjwu6A4VbeS0QHVCkhRS7fUQvi2egU3N858fiTDN6bkkOxYDVrY0Ad8L10Hs3zH81mtnPk5uvvolIC1CXGu43obcgFxeL3khZl8IKvO61GWB6jI9b5+gLPoBc1Q=", |
| 32 | + SigningCertURL: "https://sns.us-west-2.amazonaws.com/SimpleNotificationService-f3ecfb7224c7233fe7bb5f59f96de52f.pem" |
| 33 | +}; |
| 34 | + |
| 35 | +const validUnsubscribeConfirmation: UnsubscribeConfirmationMessage = { |
| 36 | + Type: "UnsubscribeConfirmation", |
| 37 | + MessageId: "165545c9-2a5c-472c-8df2-7ff2be2b3b1b", |
| 38 | + Token: "2336412f37fb687f5d51e6e241d09c805a5a57b30d712f794cc5f6a988666d92768dd60a747ba6f3beb71854e285d6ad02428b09ceece29417f1f02d609c582afbacc99c583a916b9981dd2728f4ae6fdb82efd087cc3b7849e05798d2d2785c03b0879594eeac82c01f235d0e717736", |
| 39 | + TopicArn: "arn:aws:sns:us-west-2:123456789012:MyTopic", |
| 40 | + Message: "You have chosen to deactivate subscription arn:aws:sns:us-east-1:123456789012:MyTopic:2bcfbf39-05c3-41de-beaa-fcfcc21c8f55.\nTo cancel this operation and restore the subscription, visit the SubscribeURL included in this message.", |
| 41 | + SubscribeURL: "https://sns.us-west-2.amazonaws.com/?Action=ConfirmSubscription&TopicArn=arn:aws:sns:us-west-2:123456789012:MyTopic&Token=2336412f37fb687f5d51e6e241d09c805a5a57b30d712f794cc5f6a988666d92768dd60a747ba6f3beb71854e285d6ad02428b09ceece29417f1f02d609c582afbacc99c583a916b9981dd2728f4ae6fdb82efd087cc3b7849e05798d2d2785c03b0879594eeac82c01f235d0e717736", |
| 42 | + Timestamp: "2012-04-26T20:45:04.751Z", |
| 43 | + SignatureVersion: "1", |
| 44 | + Signature: "EXAMPLEpH+DcEwjAPg8O9mY8dReBSwksfg2S7WKQcikcNKWLQjwu6A4VbeS0QHVCkhRS7fUQvi2egU3N858fiTDN6bkkOxYDVrY0Ad8L10Hs3zH81mtnPk5uvvolIC1CXGu43obcgFxeL3khZl8IKvO61GWB6jI9b5+gLPoBc1Q=", |
| 45 | + SigningCertURL: "https://sns.us-west-2.amazonaws.com/SimpleNotificationService-f3ecfb7224c7233fe7bb5f59f96de52f.pem" |
| 46 | +}; |
| 47 | + |
| 48 | +describe('isMessage', () => { |
| 49 | + it('should reject scalar values', () => { |
| 50 | + for (let scalar of ['string', 20, null, void 0, true]) { |
| 51 | + expect(isMessage(scalar)).toBe(false); |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + for (let requiredField of ['Type', 'Message', 'MessageId', 'Signature', 'SigningCertURL', 'Timestamp', 'TopicArn']) { |
| 56 | + it(`should reject messages where ${requiredField} is not defined`, () => { |
| 57 | + expect(isMessage({...validNotification, [requiredField]: void 0})) |
| 58 | + .toBe(false); |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + it( |
| 63 | + 'should reject messages whose Type is not Notification, SubscriptionConfirmation, or UnsubscribeConfirmation', |
| 64 | + () => { |
| 65 | + expect(isMessage({...validNotification, Type: 'foo'})).toBe(false); |
| 66 | + } |
| 67 | + ); |
| 68 | + |
| 69 | + it('should reject messages whose SignatureVersion is not a string', () => { |
| 70 | + expect(isMessage({...validNotification, SignatureVersion: 1})) |
| 71 | + .toBe(false); |
| 72 | + }); |
| 73 | + |
| 74 | + describe('notification messages', () => { |
| 75 | + it('should accept valid notification messages', () => { |
| 76 | + expect(isMessage(validNotification)).toBe(true); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should accept notification messages with no subject', () => { |
| 80 | + expect(isMessage({...validNotification, Subject: void 0})) |
| 81 | + .toBe(true); |
| 82 | + }); |
| 83 | + |
| 84 | + it( |
| 85 | + 'should reject notification messages whose subject is neither a string nor undefined', |
| 86 | + () => { |
| 87 | + expect(isMessage({...validNotification, Subject: 10})) |
| 88 | + .toBe(false); |
| 89 | + } |
| 90 | + ); |
| 91 | + |
| 92 | + it('should reject notification messages without an UnsubscribeURL', () => { |
| 93 | + expect(isMessage({...validNotification, UnsubscribeURL: void 0})) |
| 94 | + .toBe(false); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('subscription confirmation messages', () => { |
| 99 | + it('should accept a valid subscription confirmation message', () => { |
| 100 | + expect(isMessage(validSubscriptionConfirmation)).toBe(true); |
| 101 | + }); |
| 102 | + |
| 103 | + it( |
| 104 | + 'should reject subscription confirmation messages without a SubscribeURL', |
| 105 | + () => { |
| 106 | + expect(isMessage({ |
| 107 | + ...validSubscriptionConfirmation, |
| 108 | + SubscribeURL: void 0 |
| 109 | + })).toBe(false); |
| 110 | + } |
| 111 | + ); |
| 112 | + |
| 113 | + it( |
| 114 | + 'should reject subscription confirmation messages without a Token', |
| 115 | + () => { |
| 116 | + expect(isMessage({ |
| 117 | + ...validSubscriptionConfirmation, |
| 118 | + Token: void 0 |
| 119 | + })).toBe(false); |
| 120 | + } |
| 121 | + ); |
| 122 | + }); |
| 123 | + |
| 124 | + describe('unsubscribe confirmation messages', () => { |
| 125 | + it('should accept a valid subscription confirmation message', () => { |
| 126 | + expect(isMessage(validUnsubscribeConfirmation)).toBe(true); |
| 127 | + }); |
| 128 | + |
| 129 | + it( |
| 130 | + 'should reject subscription confirmation messages without a SubscribeURL', |
| 131 | + () => { |
| 132 | + expect(isMessage({ |
| 133 | + ...validUnsubscribeConfirmation, |
| 134 | + SubscribeURL: void 0 |
| 135 | + })).toBe(false); |
| 136 | + } |
| 137 | + ); |
| 138 | + |
| 139 | + it( |
| 140 | + 'should reject subscription confirmation messages without a Token', |
| 141 | + () => { |
| 142 | + expect(isMessage({ |
| 143 | + ...validUnsubscribeConfirmation, |
| 144 | + Token: void 0 |
| 145 | + })).toBe(false); |
| 146 | + } |
| 147 | + ); |
| 148 | + }); |
| 149 | +}); |
0 commit comments