Skip to content

Commit

Permalink
Telegram integration tests: add the possibility to disable running we…
Browse files Browse the repository at this point in the history
…bhook test

Fixes #5298
  • Loading branch information
zbendhiba committed Sep 13, 2023
1 parent d20d1f8 commit a4968d6
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 9 deletions.
9 changes: 8 additions & 1 deletion integration-tests/telegram/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,11 @@ Or

Set environment variable `WIREMOCK_RECORD=true`

Note that you'll need 2 differents bots, one dedicated to the webhook consumer only.
Note that you'll need 2 different bots, one dedicated to the webhook consumer only.

If you want to disable the webhook test, you can set the environment property

[source,shell]
----
export TELEGRAM_WEBHOOK_DISABLED=true
----
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,21 @@

import jakarta.enterprise.context.ApplicationScoped;
import org.apache.camel.builder.RouteBuilder;
import org.eclipse.microprofile.config.inject.ConfigProperty;

@ApplicationScoped
public class Routes extends RouteBuilder {

@ConfigProperty(name = "webhook.disabled")
String webhookDisabled;

@Override
public void configure() throws Exception {
from("webhook:telegram:bots?webhookPath=/my-test&webhook-external-url={{webhook-external-url}}&authorization-token={{webhook-authorization-token}}")
.log("webhook message : ${body}")
.to("mock:webhook");
boolean isWebhookDisabled = Boolean.parseBoolean(webhookDisabled);
if (!isWebhookDisabled) {
from("webhook:telegram:bots?webhookPath=/my-test&webhook-external-url={{webhook-external-url}}&authorization-token={{webhook-authorization-token}}")
.log("webhook message : ${body}")
.to("mock:webhook");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ telegram.chatId=${TELEGRAM_CHAT_ID:1426416050}

#for webhook Telegram consumer
webhook-external-url=${TELEGRAM_WEBHOOK_EXTERNAL_URL:https://my-external-link}
webhook-authorization-token=${TELEGRAM_WEBHOOK_AUTHORIZATION_TOKEN:fake-token}
webhook-authorization-token=${TELEGRAM_WEBHOOK_AUTHORIZATION_TOKEN:fake-token}
webhook.disabled=${TELEGRAM_WEBHOOK_DISABLED:false}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.quarkus.component.telegram.it;

import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;

public class CustomWebhookCondition implements ExecutionCondition {
private static final String TELEGRAM_WEBHOOK_DISABLED = "TELEGRAM_WEBHOOK_DISABLED";

@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext extensionContext) {
boolean isWebhookDisabled = System.getenv(TELEGRAM_WEBHOOK_DISABLED) != null
&& Boolean.parseBoolean(System.getenv(TELEGRAM_WEBHOOK_DISABLED));

if (isWebhookDisabled) {
return ConditionEvaluationResult.disabled("Webhook test is disabled");
}

return ConditionEvaluationResult.enabled("Webhook test is enabled");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.camel.quarkus.test.wiremock.MockServer;
import org.jboss.logging.Logger;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
Expand Down Expand Up @@ -212,6 +213,7 @@ public void venue() throws IOException {
}

@Test
@ExtendWith(CustomWebhookCondition.class)
void testWebhookEndpoint() {
//simulate POST messages from Telegram
final var message = "{\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ public class TelegramTestResource extends WireMockTestResourceLifecycleManager {
private static final String TELEGRAM_API_BASE_URL = "https://api.telegram.org";
private static final String TELEGRAM_ENV_AUTHORIZATION_TOKEN = "TELEGRAM_AUTHORIZATION_TOKEN";
private static final String TELEGRAM_ENV_CHAT_ID = "TELEGRAM_CHAT_ID";
private static final String TELEGRAM_ENV_WEBHOOK_EXTERNAL_URL = "TELEGRAM_WEBHOOK_EXTERNAL_URL";
private static final String TELEGRAM_ENV_WEBHOOK_AUTHORIZATION_TOKEN = "TELEGRAM_WEBHOOK_AUTHORIZATION_TOKEN";

@Override
public Map<String, String> start() {
Expand All @@ -45,7 +43,6 @@ protected String getRecordTargetBaseUrl() {

@Override
protected boolean isMockingEnabled() {
return !envVarsPresent(TELEGRAM_ENV_AUTHORIZATION_TOKEN, TELEGRAM_ENV_CHAT_ID, TELEGRAM_ENV_WEBHOOK_EXTERNAL_URL,
TELEGRAM_ENV_WEBHOOK_AUTHORIZATION_TOKEN);
return !envVarsPresent(TELEGRAM_ENV_AUTHORIZATION_TOKEN, TELEGRAM_ENV_CHAT_ID);
}
}

0 comments on commit a4968d6

Please sign in to comment.