|
| 1 | +// Copyright (c) 2025 WSO2 LLC. (http://www.wso2.com). |
| 2 | +// |
| 3 | +// WSO2 LLC. licenses this file to you under the Apache License, |
| 4 | +// Version 2.0 (the "License"); you may not use this file except |
| 5 | +// in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, |
| 11 | +// software distributed under the License is distributed on an |
| 12 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 13 | +// KIND, either express or implied. See the License for the |
| 14 | +// specific language governing permissions and limitations |
| 15 | +// under the License. |
| 16 | + |
| 17 | +import ballerina/test; |
| 18 | +import ballerina/http; |
| 19 | +import ballerina/lang.runtime; |
| 20 | + |
| 21 | +listener Listener autoSubscriptionVerifyListener = new(9104); |
| 22 | + |
| 23 | +http:Client subAutoVerifyClient = check new("http://localhost:9104"); |
| 24 | + |
| 25 | +const AUTO_VERIFY_TOPIC = "autoVerifyTopic"; |
| 26 | +const AUTO_VERIFY_SUBSCRIBER = "https://sample.subscriber.xyz"; |
| 27 | + |
| 28 | +isolated boolean subscriptionAutoVerified = false; |
| 29 | +isolated boolean unsubscriptionAutoVerified = false; |
| 30 | + |
| 31 | +Service autoVerifyEnabledService = @ServiceConfig { |
| 32 | + autoVerifySubscription: true |
| 33 | +} |
| 34 | +service object { |
| 35 | + |
| 36 | + isolated remote function onRegisterTopic(TopicRegistration message) returns TopicRegistrationError { |
| 37 | + return TOPIC_REGISTRATION_ERROR; |
| 38 | + } |
| 39 | + |
| 40 | + isolated remote function onDeregisterTopic(TopicDeregistration message) returns TopicDeregistrationError { |
| 41 | + return TOPIC_DEREGISTRATION_ERROR; |
| 42 | + } |
| 43 | + |
| 44 | + isolated remote function onUpdateMessage(UpdateMessage msg) returns UpdateMessageError { |
| 45 | + return UPDATE_MESSAGE_ERROR; |
| 46 | + } |
| 47 | + |
| 48 | + isolated remote function onSubscription(Subscription msg, http:Headers headers, Controller hubController) |
| 49 | + returns SubscriptionAccepted|InternalSubscriptionError { |
| 50 | + Error? result = hubController.markAsVerified(msg); |
| 51 | + if result is Error { |
| 52 | + return INTERNAL_SUBSCRIPTION_ERROR; |
| 53 | + } |
| 54 | + |
| 55 | + return SUBSCRIPTION_ACCEPTED; |
| 56 | + } |
| 57 | + |
| 58 | + isolated remote function onSubscriptionIntentVerified(VerifiedSubscription msg) { |
| 59 | + if AUTO_VERIFY_TOPIC == msg.hubTopic && AUTO_VERIFY_SUBSCRIBER == msg.hubCallback { |
| 60 | + lock { |
| 61 | + subscriptionAutoVerified = true; |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + isolated remote function onUnsubscription(Unsubscription msg, http:Headers headers, Controller hubController) |
| 67 | + returns UnsubscriptionAccepted|InternalUnsubscriptionError { |
| 68 | + Error? result = hubController.markAsVerified(msg); |
| 69 | + if result is Error { |
| 70 | + return INTERNAL_UNSUBSCRIPTION_ERROR; |
| 71 | + } |
| 72 | + |
| 73 | + return UNSUBSCRIPTION_ACCEPTED; |
| 74 | + } |
| 75 | + |
| 76 | + isolated remote function onUnsubscriptionIntentVerified(VerifiedUnsubscription msg){ |
| 77 | + if AUTO_VERIFY_TOPIC == msg.hubTopic && AUTO_VERIFY_SUBSCRIBER == msg.hubCallback { |
| 78 | + lock { |
| 79 | + unsubscriptionAutoVerified = true; |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +}; |
| 84 | + |
| 85 | +Service autoVerifyDisabledService = service object { |
| 86 | + |
| 87 | + isolated remote function onRegisterTopic(TopicRegistration message) returns TopicRegistrationError { |
| 88 | + return TOPIC_REGISTRATION_ERROR; |
| 89 | + } |
| 90 | + |
| 91 | + isolated remote function onDeregisterTopic(TopicDeregistration message) returns TopicDeregistrationError { |
| 92 | + return TOPIC_DEREGISTRATION_ERROR; |
| 93 | + } |
| 94 | + |
| 95 | + isolated remote function onUpdateMessage(UpdateMessage msg) returns UpdateMessageError { |
| 96 | + return UPDATE_MESSAGE_ERROR; |
| 97 | + } |
| 98 | + |
| 99 | + isolated remote function onSubscription(Subscription msg, http:Headers headers, Controller hubController) |
| 100 | + returns SubscriptionAccepted|InternalSubscriptionError { |
| 101 | + Error? result = hubController.markAsVerified(msg); |
| 102 | + if result is Error { |
| 103 | + return INTERNAL_SUBSCRIPTION_ERROR; |
| 104 | + } |
| 105 | + |
| 106 | + return SUBSCRIPTION_ACCEPTED; |
| 107 | + } |
| 108 | + |
| 109 | + isolated remote function onSubscriptionIntentVerified(VerifiedSubscription msg) {} |
| 110 | + |
| 111 | + isolated remote function onUnsubscription(Unsubscription msg, http:Headers headers, Controller hubController) |
| 112 | + returns UnsubscriptionAccepted|InternalUnsubscriptionError { |
| 113 | + Error? result = hubController.markAsVerified(msg); |
| 114 | + if result is Error { |
| 115 | + return INTERNAL_UNSUBSCRIPTION_ERROR; |
| 116 | + } |
| 117 | + |
| 118 | + return UNSUBSCRIPTION_ACCEPTED; |
| 119 | + } |
| 120 | + |
| 121 | + isolated remote function onUnsubscriptionIntentVerified(VerifiedUnsubscription msg) {} |
| 122 | +}; |
| 123 | + |
| 124 | +@test:BeforeGroups { value:["autoVerifySubscription"] } |
| 125 | +function beforeAutoVerifySubscriptionTest() returns error? { |
| 126 | + check autoSubscriptionVerifyListener.attach(autoVerifyEnabledService, "websubhub"); |
| 127 | + check autoSubscriptionVerifyListener.attach(autoVerifyDisabledService, "websubhubnegative"); |
| 128 | +} |
| 129 | + |
| 130 | +@test:AfterGroups { value:["autoVerifySubscription"] } |
| 131 | +function afterAutoVerifySubscriptionTest() returns error? { |
| 132 | + check autoSubscriptionVerifyListener.gracefulStop(); |
| 133 | +} |
| 134 | + |
| 135 | +@test:Config{ |
| 136 | + groups: ["autoVerifySubscription"] |
| 137 | +} |
| 138 | +function testSubscriptionWithAutoVerification() returns error? { |
| 139 | + http:Request request = new; |
| 140 | + request.setTextPayload("hub.mode=subscribe&hub.topic=autoVerifyTopic&hub.callback=https://sample.subscriber.xyz", |
| 141 | + "application/x-www-form-urlencoded"); |
| 142 | + http:Response response = check subAutoVerifyClient->post("/websubhub", request); |
| 143 | + test:assertEquals(response.statusCode, 202); |
| 144 | + |
| 145 | + runtime:sleep(2); |
| 146 | + boolean subscriptionVerified = false; |
| 147 | + lock { |
| 148 | + subscriptionVerified = subscriptionAutoVerified; |
| 149 | + } |
| 150 | + test:assertTrue(subscriptionVerified); |
| 151 | +} |
| 152 | + |
| 153 | +@test:Config{ |
| 154 | + groups: ["autoVerifySubscription"] |
| 155 | +} |
| 156 | +function testUnsubscriptionWithAutoVerification() returns error? { |
| 157 | + http:Request request = new; |
| 158 | + request.setTextPayload("hub.mode=unsubscribe&hub.topic=autoVerifyTopic&hub.callback=https://sample.subscriber.xyz", |
| 159 | + "application/x-www-form-urlencoded"); |
| 160 | + http:Response response = check subAutoVerifyClient->post("/websubhub", request); |
| 161 | + test:assertEquals(response.statusCode, 202); |
| 162 | + |
| 163 | + runtime:sleep(2); |
| 164 | + boolean unsubscriptionVerified = false; |
| 165 | + lock { |
| 166 | + unsubscriptionVerified = unsubscriptionAutoVerified; |
| 167 | + } |
| 168 | + test:assertTrue(unsubscriptionVerified); |
| 169 | +} |
| 170 | + |
| 171 | +@test:Config{ |
| 172 | + groups: ["autoVerifySubscription"] |
| 173 | +} |
| 174 | +function testSubscriptionWithAutoVerificationError() returns error? { |
| 175 | + http:Request request = new; |
| 176 | + request.setTextPayload("hub.mode=subscribe&hub.topic=autoVerifyTopic&hub.callback=https://sample.subscriber.xyz", |
| 177 | + "application/x-www-form-urlencoded"); |
| 178 | + http:Response response = check subAutoVerifyClient->post("/websubhubnegative", request); |
| 179 | + test:assertEquals(response.statusCode, 500); |
| 180 | +} |
| 181 | + |
| 182 | +@test:Config{ |
| 183 | + groups: ["autoVerifySubscription"] |
| 184 | +} |
| 185 | +function testUnsubscriptionWithAutoVerificationError() returns error? { |
| 186 | + http:Request request = new; |
| 187 | + request.setTextPayload("hub.mode=unsubscribe&hub.topic=autoVerifyTopic&hub.callback=https://sample.subscriber.xyz", |
| 188 | + "application/x-www-form-urlencoded"); |
| 189 | + http:Response response = check subAutoVerifyClient->post("/websubhubnegative", request); |
| 190 | + test:assertEquals(response.statusCode, 500); |
| 191 | +} |
0 commit comments