github.com/anycable/anycable-go@v1.5.1/identity/identity_test.go (about)

     1  package identity
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/anycable/anycable-go/common"
     8  	"github.com/anycable/anycable-go/mocks"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestIdentifiableController(t *testing.T) {
    13  	controller := mocks.Controller{}
    14  	identifier := mocks.Identifier{}
    15  	env := common.NewSessionEnv("ws://demo.anycable.io/cable", &map[string]string{"cookie": "val=1;"})
    16  	commandResult := &common.CommandResult{Transmissions: []string{"message_sent"}, Streams: []string{"chat_42"}}
    17  
    18  	subject := NewIdentifiableController(&controller, &identifier)
    19  
    20  	t.Run("Start", func(t *testing.T) {
    21  		controller.On("Start").Return(nil)
    22  
    23  		assert.Nil(t, subject.Start())
    24  
    25  		controller.AssertCalled(t, "Start")
    26  	})
    27  
    28  	t.Run("Shutdown", func(t *testing.T) {
    29  		controller.On("Shutdown").Return(nil)
    30  
    31  		assert.Nil(t, subject.Shutdown())
    32  
    33  		controller.AssertCalled(t, "Shutdown")
    34  	})
    35  
    36  	t.Run("Authenticate (success)", func(t *testing.T) {
    37  		expected := &common.ConnectResult{
    38  			Identifier:         "test_ids",
    39  			Transmissions:      []string{`{"type":"welcome","sid":"2021"}`},
    40  			DisconnectInterest: -1,
    41  			Status:             common.SUCCESS,
    42  		}
    43  
    44  		controller.On("Authenticate", "2021", env).Return(nil, errors.New("shouldn't be called"))
    45  		identifier.On("Identify", "2021", env).Return(expected, nil)
    46  
    47  		res, err := subject.Authenticate("2021", env)
    48  
    49  		assert.NoError(t, err)
    50  		assert.Equal(t, expected, res)
    51  		controller.AssertNotCalled(t, "Authenticate", "2021", env)
    52  	})
    53  
    54  	t.Run("Authenticate (failure)", func(t *testing.T) {
    55  		expected := &common.ConnectResult{Status: common.FAILURE}
    56  
    57  		controller.On("Authenticate", "2020", env).Return(nil, errors.New("shouldn't be called"))
    58  		identifier.On("Identify", "2020", env).Return(expected, nil)
    59  
    60  		res, err := subject.Authenticate("2020", env)
    61  
    62  		assert.NoError(t, err)
    63  		assert.Equal(t, expected, res)
    64  		controller.AssertNotCalled(t, "Authenticate", "2020", env)
    65  	})
    66  
    67  	t.Run("Authenticate (error)", func(t *testing.T) {
    68  		expectedErr := errors.New("identifier failed")
    69  
    70  		controller.On("Authenticate", "1998", env).Return(nil, errors.New("shouldn't be called"))
    71  		identifier.On("Identify", "1998", env).Return(nil, expectedErr)
    72  
    73  		res, err := subject.Authenticate("1998", env)
    74  
    75  		assert.Nil(t, res)
    76  		assert.Equal(t, expectedErr, err)
    77  		controller.AssertNotCalled(t, "Authenticate", "1998", env)
    78  
    79  	})
    80  
    81  	t.Run("Authenticate (noop -> passthrough)", func(t *testing.T) {
    82  		expected := &common.ConnectResult{Identifier: "test_ids", Transmissions: []string{`{"type":"welcome","sid":"2022"}`}, Status: common.SUCCESS}
    83  
    84  		controller.On("Authenticate", "2022", env).Return(expected, nil)
    85  		identifier.On("Identify", "2022", env).Return(nil, nil)
    86  
    87  		res, err := subject.Authenticate("2022", env)
    88  
    89  		assert.NoError(t, err)
    90  		assert.Equal(t, expected, res)
    91  		controller.AssertCalled(t, "Authenticate", "2022", env)
    92  	})
    93  
    94  	t.Run("Subscribe", func(t *testing.T) {
    95  		controller.On("Subscribe", "42", env, "name=jack", "chat").Return(commandResult, nil)
    96  
    97  		res, err := subject.Subscribe("42", env, "name=jack", "chat")
    98  
    99  		assert.NoError(t, err)
   100  		assert.Equal(t, commandResult, res)
   101  
   102  		controller.AssertCalled(t, "Subscribe", "42", env, "name=jack", "chat")
   103  	})
   104  
   105  	t.Run("Unsubscribe", func(t *testing.T) {
   106  		controller.On("Unsubscribe", "42", env, "name=jack", "chat").Return(commandResult, nil)
   107  
   108  		res, err := subject.Unsubscribe("42", env, "name=jack", "chat")
   109  
   110  		assert.NoError(t, err)
   111  		assert.Equal(t, commandResult, res)
   112  
   113  		controller.AssertCalled(t, "Unsubscribe", "42", env, "name=jack", "chat")
   114  	})
   115  
   116  	t.Run("Perform", func(t *testing.T) {
   117  		controller.On("Perform", "42", env, "name=jack", "chat", "ping").Return(commandResult, nil)
   118  
   119  		res, err := subject.Perform("42", env, "name=jack", "chat", "ping")
   120  
   121  		assert.NoError(t, err)
   122  		assert.Equal(t, commandResult, res)
   123  
   124  		controller.AssertCalled(t, "Perform", "42", env, "name=jack", "chat", "ping")
   125  	})
   126  
   127  	t.Run("Disconnect", func(t *testing.T) {
   128  		expectedErr := errors.New("foo")
   129  		controller.On("Disconnect", "42", env, "name=jack", []string{"chat"}).Return(expectedErr)
   130  
   131  		err := subject.Disconnect("42", env, "name=jack", []string{"chat"})
   132  
   133  		assert.Equal(t, expectedErr, err)
   134  
   135  		controller.AssertCalled(t, "Disconnect", "42", env, "name=jack", []string{"chat"})
   136  	})
   137  }
   138  
   139  func TestIdentifierPipeline(t *testing.T) {
   140  	mocked := mocks.Identifier{}
   141  	pub := NewPublicIdentifier()
   142  
   143  	pipe := NewIdentifierPipeline(&mocked, pub)
   144  
   145  	env := common.NewSessionEnv("ws://demo.anycable.io/cable", &map[string]string{"cookie": "val=1;"})
   146  
   147  	t.Run("first identifier success", func(t *testing.T) {
   148  		mocked.On("Identify", "mock-ok", env).Return(
   149  			&common.ConnectResult{Identifier: "mock_welcome"},
   150  			nil,
   151  		)
   152  
   153  		res, err := pipe.Identify("mock-ok", env)
   154  
   155  		assert.NoError(t, err)
   156  		assert.Equal(t, "mock_welcome", res.Identifier)
   157  	})
   158  
   159  	t.Run("second identifier success", func(t *testing.T) {
   160  		mocked.On("Identify", "mock-nok", env).Return(nil, nil)
   161  
   162  		res, err := pipe.Identify("mock-nok", env)
   163  
   164  		assert.NoError(t, err)
   165  		assert.Equal(t, `{"sid":"mock-nok"}`, res.Identifier)
   166  	})
   167  
   168  	t.Run("first identifier fail", func(t *testing.T) {
   169  		mocked.On("Identify", "mock-err", env).Return(nil, errors.New("failed"))
   170  
   171  		_, err := pipe.Identify("mock-err", env)
   172  
   173  		assert.Error(t, err)
   174  	})
   175  }