Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
test(sdk): pin every field of the key validation answer in Go and Java
Yudhi Armyndharis16d61b4a3393
diff --git a/sdk/go/auth_validate_test.go b/sdk/go/auth_validate_test.goindex f7a5dd916..108945929 100644--- a/sdk/go/auth_validate_test.go+++ b/sdk/go/auth_validate_test.go@@ -1,6 +1,7 @@package openwaimport (+ "context""encoding/json""testing")@@ -15,3 +16,30 @@ func TestAuthValidateResponseDecodesScoped(t *testing.T) {t.Errorf("Scoped = false, want true")}}++// Every key of the validate answer reaches its field: a renamed JSON tag would read the key as invalid, or+// drop its role, without any other test noticing.+func TestAuthValidateDecodesEveryField(t *testing.T) {+ rt := &recordTransport{status: 200, body: `{"valid":true,"role":"operator","engineType":"baileys","scoped":true}`}+ c := newTestClient(t, rt)+ out, err := c.Auth.Validate(context.Background())+ if err != nil {+ t.Fatal(err)+ }+ if rt.lastReq.Method != "POST" || rt.lastReq.URL.Path != "/api/auth/validate" {+ t.Errorf("request = %s %s, want POST /api/auth/validate", rt.lastReq.Method, rt.lastReq.URL.Path)+ }+ if !out.Valid || out.Role != "operator" || out.EngineType != "baileys" || !out.Scoped {+ t.Errorf("decoded %+v, want valid, role operator, engineType baileys, scoped", *out)+ }++ // A key the gateway refuses is answered with the one key.+ rt.body = `{"valid":false}`+ out, err = c.Auth.Validate(context.Background())+ if err != nil {+ t.Fatal(err)+ }+ if out.Valid || out.Role != "" || out.EngineType != "" || out.Scoped {+ t.Errorf("decoded %+v, want an invalid key with nothing else", *out)+ }+}diff --git a/sdk/java/src/test/java/com/rmyndharis/openwa/ClientTest.java b/sdk/java/src/test/java/com/rmyndharis/openwa/ClientTest.javaindex 7538e6629..92a7da00d 100644--- a/sdk/java/src/test/java/com/rmyndharis/openwa/ClientTest.java+++ b/sdk/java/src/test/java/com/rmyndharis/openwa/ClientTest.java@@ -2,6 +2,7 @@ package com.rmyndharis.openwa;import static org.junit.jupiter.api.Assertions.assertArrayEquals;import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertFalse;import static org.junit.jupiter.api.Assertions.assertNull;import static org.junit.jupiter.api.Assertions.assertThrows;import static org.junit.jupiter.api.Assertions.assertTrue;@@ -12,6 +13,7 @@ import com.rmyndharis.openwa.errors.OpenWANotFoundError;import com.rmyndharis.openwa.errors.OpenWARateLimitError;import com.rmyndharis.openwa.http.BinaryResponse;import com.rmyndharis.openwa.http.HttpMethod;+import com.rmyndharis.openwa.model.AuthValidateResponse;import com.rmyndharis.openwa.model.SuccessResult;import com.rmyndharis.openwa.support.MockTransport;import java.nio.charset.StandardCharsets;@@ -126,12 +128,31 @@ class ClientTest {@Testvoid authPostsToValidatePath() {- tx.respond(200, "{\"valid\":true,\"role\":\"OPERATOR\",\"scoped\":true}");+ tx.respond(200, "{\"valid\":true,\"role\":\"operator\",\"scoped\":true}");assertEquals(Boolean.TRUE, client.auth().scoped());assertEquals("http://h:2785/api/auth/validate", tx.lastRequest().url());assertEquals(HttpMethod.POST, tx.lastRequest().method());}+ @Test+ void authDecodesEveryField() {+ // A renamed component would read a valid key as invalid or drop its role without any other test noticing.+ tx.respond(200, "{\"valid\":true,\"role\":\"operator\",\"engineType\":\"baileys\",\"scoped\":false}");+ AuthValidateResponse ok = client.auth();+ assertTrue(ok.valid());+ assertEquals("operator", ok.role());+ assertEquals("baileys", ok.engineType());+ assertEquals(Boolean.FALSE, ok.scoped());++ // A key the gateway refuses is answered with the one key.+ tx.respond(200, "{\"valid\":false}");+ AuthValidateResponse refused = client.auth();+ assertFalse(refused.valid());+ assertNull(refused.role());+ assertNull(refused.engineType());+ assertNull(refused.scoped());+ }+@Testvoid emptyBodyReturnsNull() {tx.respond(204, "");