github.com/livekit/protocol@v1.39.3/rpc/sip_test.go (about)

     1  package rpc
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  
     8  	"github.com/livekit/protocol/livekit"
     9  )
    10  
    11  func TestNewCreateSIPParticipantRequest(t *testing.T) {
    12  	r := &livekit.CreateSIPParticipantRequest{
    13  		SipCallTo:           "+3333",
    14  		RoomName:            "room",
    15  		ParticipantIdentity: "",
    16  		ParticipantName:     "",
    17  		ParticipantMetadata: "meta",
    18  		ParticipantAttributes: map[string]string{
    19  			"extra": "1",
    20  		},
    21  		Headers: map[string]string{
    22  			"X-B": "B2",
    23  			"X-C": "C",
    24  		},
    25  		Dtmf:              "1234#",
    26  		PlayDialtone:      true,
    27  		WaitUntilAnswered: true,
    28  	}
    29  	tr := &livekit.SIPOutboundTrunkInfo{
    30  		SipTrunkId:         "trunk",
    31  		Address:            "sip.example.com",
    32  		Numbers:            []string{"+1111"},
    33  		DestinationCountry: "us",
    34  		AuthUsername:       "user",
    35  		AuthPassword:       "pass",
    36  		Headers: map[string]string{
    37  			"X-A": "A",
    38  			"X-B": "B1",
    39  		},
    40  	}
    41  	exp := &InternalCreateSIPParticipantRequest{
    42  		ProjectId:           "p_123",
    43  		SipCallId:           "call-id",
    44  		SipTrunkId:          "trunk",
    45  		Address:             "sip.example.com",
    46  		Hostname:            "xyz.sip.livekit.cloud",
    47  		DestinationCountry:  "us",
    48  		Number:              "+1111",
    49  		CallTo:              "+3333",
    50  		Username:            "user",
    51  		Password:            "pass",
    52  		RoomName:            "room",
    53  		ParticipantIdentity: "sip_+3333",
    54  		ParticipantMetadata: "meta",
    55  		Token:               "token",
    56  		WsUrl:               "url",
    57  		Dtmf:                "1234#",
    58  		PlayDialtone:        true,
    59  		ParticipantAttributes: map[string]string{
    60  			"extra":                    "1",
    61  			livekit.AttrSIPCallID:      "call-id",
    62  			livekit.AttrSIPTrunkID:     "trunk",
    63  			livekit.AttrSIPTrunkNumber: "+1111",
    64  			livekit.AttrSIPPhoneNumber: "+3333",
    65  			livekit.AttrSIPHostName:    "sip.example.com",
    66  		},
    67  		Headers: map[string]string{
    68  			"X-A": "A",
    69  			"X-B": "B2",
    70  			"X-C": "C",
    71  		},
    72  		WaitUntilAnswered: true,
    73  	}
    74  	res, err := NewCreateSIPParticipantRequest("p_123", "call-id", "xyz.sip.livekit.cloud", "url", "token", r, tr)
    75  	require.NoError(t, err)
    76  	require.Equal(t, exp, res)
    77  
    78  	r.HidePhoneNumber = true
    79  	res, err = NewCreateSIPParticipantRequest("p_123", "call-id", "xyz.sip.livekit.cloud", "url", "token", r, tr)
    80  	require.NoError(t, err)
    81  	require.Equal(t, &InternalCreateSIPParticipantRequest{
    82  		ProjectId:           "p_123",
    83  		SipCallId:           "call-id",
    84  		SipTrunkId:          "trunk",
    85  		Address:             "sip.example.com",
    86  		Hostname:            "xyz.sip.livekit.cloud",
    87  		DestinationCountry:  "us",
    88  		Number:              "+1111",
    89  		CallTo:              "+3333",
    90  		Username:            "user",
    91  		Password:            "pass",
    92  		RoomName:            "room",
    93  		Token:               "token",
    94  		WsUrl:               "url",
    95  		Dtmf:                "1234#",
    96  		PlayDialtone:        true,
    97  		ParticipantIdentity: "sip_+3333",
    98  		ParticipantAttributes: map[string]string{
    99  			"extra":                "1",
   100  			livekit.AttrSIPCallID:  "call-id",
   101  			livekit.AttrSIPTrunkID: "trunk",
   102  		},
   103  		ParticipantMetadata: "meta",
   104  		Headers: map[string]string{
   105  			"X-A": "A",
   106  			"X-B": "B2",
   107  			"X-C": "C",
   108  		},
   109  		WaitUntilAnswered: true,
   110  	}, res)
   111  
   112  	r.HidePhoneNumber = false
   113  	r.SipNumber = tr.Numbers[0]
   114  	r.Trunk = &livekit.SIPOutboundConfig{
   115  		Hostname:            tr.Address,
   116  		Transport:           tr.Transport,
   117  		DestinationCountry:  "us",
   118  		AuthUsername:        tr.AuthUsername,
   119  		AuthPassword:        tr.AuthPassword,
   120  		HeadersToAttributes: tr.HeadersToAttributes,
   121  		AttributesToHeaders: tr.AttributesToHeaders,
   122  	}
   123  	r.SipTrunkId = ""
   124  	exp.SipTrunkId = ""
   125  	for k, v := range tr.Headers {
   126  		if _, ok := r.Headers[k]; !ok {
   127  			r.Headers[k] = v
   128  		}
   129  	}
   130  	exp.ParticipantAttributes[livekit.AttrSIPTrunkID] = ""
   131  	res, err = NewCreateSIPParticipantRequest("p_123", "call-id", "xyz.sip.livekit.cloud", "url", "token", r, nil)
   132  	require.NoError(t, err)
   133  	require.Equal(t, exp, res)
   134  }