github.com/rajatvaryani/mattermost-server@v5.11.1+incompatible/model/integration_action_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package model
     5  
     6  import (
     7  	"bytes"
     8  	"crypto/ecdsa"
     9  	"crypto/elliptic"
    10  	"crypto/rand"
    11  	"encoding/base64"
    12  	"strings"
    13  	"testing"
    14  
    15  	"github.com/stretchr/testify/assert"
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  func TestTriggerIdDecodeAndVerification(t *testing.T) {
    20  
    21  	key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
    22  	require.Nil(t, err)
    23  
    24  	t.Run("should succeed decoding and validation", func(t *testing.T) {
    25  		userId := NewId()
    26  		clientTriggerId, triggerId, err := GenerateTriggerId(userId, key)
    27  		decodedClientTriggerId, decodedUserId, err := DecodeAndVerifyTriggerId(triggerId, key)
    28  		assert.Nil(t, err)
    29  		assert.Equal(t, clientTriggerId, decodedClientTriggerId)
    30  		assert.Equal(t, userId, decodedUserId)
    31  	})
    32  
    33  	t.Run("should succeed decoding and validation through request structs", func(t *testing.T) {
    34  		actionReq := &PostActionIntegrationRequest{
    35  			UserId: NewId(),
    36  		}
    37  		clientTriggerId, triggerId, err := actionReq.GenerateTriggerId(key)
    38  		dialogReq := &OpenDialogRequest{TriggerId: triggerId}
    39  		decodedClientTriggerId, decodedUserId, err := dialogReq.DecodeAndVerifyTriggerId(key)
    40  		assert.Nil(t, err)
    41  		assert.Equal(t, clientTriggerId, decodedClientTriggerId)
    42  		assert.Equal(t, actionReq.UserId, decodedUserId)
    43  	})
    44  
    45  	t.Run("should fail on base64 decode", func(t *testing.T) {
    46  		_, _, err := DecodeAndVerifyTriggerId("junk!", key)
    47  		require.NotNil(t, err)
    48  		assert.Equal(t, "interactive_message.decode_trigger_id.base64_decode_failed", err.Id)
    49  	})
    50  
    51  	t.Run("should fail on trigger parsing", func(t *testing.T) {
    52  		_, _, err := DecodeAndVerifyTriggerId(base64.StdEncoding.EncodeToString([]byte("junk!")), key)
    53  		require.NotNil(t, err)
    54  		assert.Equal(t, "interactive_message.decode_trigger_id.missing_data", err.Id)
    55  	})
    56  
    57  	t.Run("should fail on expired timestamp", func(t *testing.T) {
    58  		_, _, err := DecodeAndVerifyTriggerId(base64.StdEncoding.EncodeToString([]byte("some-trigger-id:some-user-id:1234567890:junksignature")), key)
    59  		require.NotNil(t, err)
    60  		assert.Equal(t, "interactive_message.decode_trigger_id.expired", err.Id)
    61  	})
    62  
    63  	t.Run("should fail on base64 decoding signature", func(t *testing.T) {
    64  		_, _, err := DecodeAndVerifyTriggerId(base64.StdEncoding.EncodeToString([]byte("some-trigger-id:some-user-id:12345678900000:junk!")), key)
    65  		require.NotNil(t, err)
    66  		assert.Equal(t, "interactive_message.decode_trigger_id.base64_decode_failed_signature", err.Id)
    67  	})
    68  
    69  	t.Run("should fail on bad signature", func(t *testing.T) {
    70  		_, _, err := DecodeAndVerifyTriggerId(base64.StdEncoding.EncodeToString([]byte("some-trigger-id:some-user-id:12345678900000:junk")), key)
    71  		require.NotNil(t, err)
    72  		assert.Equal(t, "interactive_message.decode_trigger_id.signature_decode_failed", err.Id)
    73  	})
    74  
    75  	t.Run("should fail on bad key", func(t *testing.T) {
    76  		_, triggerId, err := GenerateTriggerId(NewId(), key)
    77  		newKey, keyErr := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
    78  		require.Nil(t, keyErr)
    79  		_, _, err = DecodeAndVerifyTriggerId(triggerId, newKey)
    80  		require.NotNil(t, err)
    81  		assert.Equal(t, "interactive_message.decode_trigger_id.verify_signature_failed", err.Id)
    82  	})
    83  }
    84  
    85  func TestPostActionIntegrationRequestToJson(t *testing.T) {
    86  	o := PostActionIntegrationRequest{UserId: NewId(), Context: StringInterface{"a": "abc"}}
    87  	j := o.ToJson()
    88  	ro := PostActionIntegrationRequestFromJson(bytes.NewReader(j))
    89  
    90  	assert.NotNil(t, ro)
    91  	assert.Equal(t, o, *ro)
    92  }
    93  
    94  func TestPostActionIntegrationRequestFromJsonError(t *testing.T) {
    95  	ro := PostActionIntegrationRequestFromJson(strings.NewReader(""))
    96  	assert.Nil(t, ro)
    97  }
    98  
    99  func TestPostActionIntegrationResponseToJson(t *testing.T) {
   100  	o := PostActionIntegrationResponse{Update: &Post{Id: NewId(), Message: NewId()}, EphemeralText: NewId()}
   101  	j := o.ToJson()
   102  	ro := PostActionIntegrationResponseFromJson(bytes.NewReader(j))
   103  
   104  	assert.NotNil(t, ro)
   105  	assert.Equal(t, o, *ro)
   106  }
   107  
   108  func TestPostActionIntegrationResponseFromJsonError(t *testing.T) {
   109  	ro := PostActionIntegrationResponseFromJson(strings.NewReader(""))
   110  	assert.Nil(t, ro)
   111  }
   112  
   113  func TestSubmitDialogRequestToJson(t *testing.T) {
   114  	t.Run("all fine", func(t *testing.T) {
   115  		request := SubmitDialogRequest{
   116  			URL:        "http://example.org",
   117  			CallbackId: NewId(),
   118  			State:      "some state",
   119  			UserId:     NewId(),
   120  			ChannelId:  NewId(),
   121  			TeamId:     NewId(),
   122  			Submission: map[string]interface{}{
   123  				"text":  "some text",
   124  				"float": 1.2,
   125  				"bool":  true,
   126  			},
   127  			Cancelled: true,
   128  		}
   129  		jsonRequest := request.ToJson()
   130  		r := SubmitDialogRequestFromJson(bytes.NewReader(jsonRequest))
   131  
   132  		require.NotNil(t, r)
   133  		assert.Equal(t, request, *r)
   134  	})
   135  	t.Run("error", func(t *testing.T) {
   136  		r := SubmitDialogRequestFromJson(strings.NewReader(""))
   137  		assert.Nil(t, r)
   138  	})
   139  }
   140  
   141  func TestSubmitDialogResponseToJson(t *testing.T) {
   142  	t.Run("all fine", func(t *testing.T) {
   143  		request := SubmitDialogResponse{
   144  			Errors: map[string]string{
   145  				"text":  "some text",
   146  				"float": "1.2",
   147  				"bool":  "true",
   148  			},
   149  		}
   150  		jsonRequest := request.ToJson()
   151  		r := SubmitDialogResponseFromJson(bytes.NewReader(jsonRequest))
   152  
   153  		require.NotNil(t, r)
   154  		assert.Equal(t, request, *r)
   155  	})
   156  	t.Run("error", func(t *testing.T) {
   157  		r := SubmitDialogResponseFromJson(strings.NewReader(""))
   158  		assert.Nil(t, r)
   159  	})
   160  }