github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/core/quality/sender_test.go (about)

     1  /*
     2   * Copyright (C) 2019 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package quality
    19  
    20  import (
    21  	"errors"
    22  	"runtime"
    23  	"testing"
    24  
    25  	"github.com/mysteriumnetwork/node/identity"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  type mockEventsTransport struct {
    30  	sentEvent    Event
    31  	mockResponse error
    32  }
    33  
    34  func buildMockEventsTransport(mockResponse error) *mockEventsTransport {
    35  	return &mockEventsTransport{mockResponse: mockResponse}
    36  }
    37  
    38  func (transport *mockEventsTransport) SendEvent(event Event) error {
    39  	transport.sentEvent = event
    40  	return transport.mockResponse
    41  }
    42  
    43  func TestSender_SendStartupEvent_SendsToTransport(t *testing.T) {
    44  	mockTransport := buildMockEventsTransport(nil)
    45  	sender := &Sender{Transport: mockTransport, AppVersion: "test version"}
    46  
    47  	sender.sendUnlockEvent(identity.AppEventIdentityUnlock{ID: identity.FromAddress("0x1234567890abcdef")})
    48  
    49  	sentEvent := mockTransport.sentEvent
    50  	assert.Equal(t, "unlock", sentEvent.EventName)
    51  	assert.Equal(t, appInfo{Name: "myst", Version: "test version", OS: runtime.GOOS, Arch: runtime.GOARCH}, sentEvent.Application)
    52  	assert.NotZero(t, sentEvent.CreatedAt)
    53  }
    54  
    55  func TestSender_SendNATMappingSuccessEvent_SendsToTransport(t *testing.T) {
    56  	mockTransport := buildMockEventsTransport(nil)
    57  	sender := &Sender{Transport: mockTransport, AppVersion: "test version"}
    58  
    59  	sender.SendNATMappingSuccessEvent("id", "port_mapping", nil)
    60  
    61  	sentEvent := mockTransport.sentEvent
    62  	assert.Equal(t, "nat_mapping", sentEvent.EventName)
    63  	assert.Equal(t, appInfo{Name: "myst", Version: "test version", OS: runtime.GOOS, Arch: runtime.GOARCH}, sentEvent.Application)
    64  	assert.NotZero(t, sentEvent.CreatedAt)
    65  	assert.Equal(t, natMappingContext{ID: "id", Successful: true, Stage: "port_mapping"}, sentEvent.Context)
    66  }
    67  
    68  func TestSender_SendNATMappingFailEvent_SendsToTransport(t *testing.T) {
    69  	mockTransport := buildMockEventsTransport(nil)
    70  
    71  	mockGateways := []map[string]string{
    72  		{"test": "test"},
    73  	}
    74  	mockError := errors.New("mock nat mapping error")
    75  
    76  	sender := &Sender{Transport: mockTransport, AppVersion: "test version"}
    77  	sender.SendNATMappingFailEvent("id", "hole_punching", mockGateways, mockError)
    78  
    79  	sentEvent := mockTransport.sentEvent
    80  	assert.Equal(t, "nat_mapping", sentEvent.EventName)
    81  	assert.Equal(t, appInfo{Name: "myst", Version: "test version", OS: runtime.GOOS, Arch: runtime.GOARCH}, sentEvent.Application)
    82  	assert.NotZero(t, sentEvent.CreatedAt)
    83  	c := sentEvent.Context.(natMappingContext)
    84  	assert.False(t, c.Successful)
    85  	assert.Equal(t, "mock nat mapping error", *c.ErrorMessage)
    86  	assert.Equal(t, "hole_punching", c.Stage)
    87  	assert.Equal(t, "id", c.ID)
    88  	assert.Equal(t, mockGateways, c.Gateways)
    89  }