github.com/anycable/anycable-go@v1.5.1/utils/message_verifier_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestMessageVerifier(t *testing.T) {
    11  	verifier := NewMessageVerifier("s3Krit")
    12  
    13  	// Turbo.signed_stream_verifier_key = 's3Krit'
    14  	// Turbo::StreamsChannel.signed_stream_name([:chat, "2021"])
    15  	example := "ImNoYXQ6MjAyMSI=--f9ee45dbccb1da04d8ceb99cc820207804370ba0d06b46fc3b8b373af1315628"
    16  
    17  	generated, err := verifier.Generate("chat:2021")
    18  	require.NoError(t, err)
    19  
    20  	assert.Equal(t, example, generated)
    21  
    22  	res, err := verifier.Verified(example)
    23  
    24  	assert.NoError(t, err)
    25  	assert.Equal(t, "chat:2021", res)
    26  
    27  	a_verifier := NewMessageVerifier("secret")
    28  	a_generated, _ := a_verifier.Generate("chat:2021")
    29  
    30  	_, err = verifier.Verified(a_generated)
    31  	assert.Error(t, err)
    32  }
    33  
    34  func TestMessageVerifierNotString(t *testing.T) {
    35  	verifier := NewMessageVerifier("s3Krit")
    36  	example := "WyJjaGF0LzIwMjMiLDE2ODUwMjQwMTdd--5b6661024d4c463c4936cd1542bc9a7672dd8039ac407d0b6c901697190e8aeb"
    37  
    38  	res, err := verifier.Verified(example)
    39  
    40  	arr := res.([]interface{})
    41  
    42  	assert.NoError(t, err)
    43  	assert.Equal(t, "chat/2023", arr[0])
    44  }