github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/model/push_notification_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  	"strings"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestPushNotification(t *testing.T) {
    14  	t.Run("should build a push notification from JSON", func(t *testing.T) {
    15  		msg := PushNotification{Platform: "test"}
    16  		json := msg.ToJson()
    17  		result, err := PushNotificationFromJson(strings.NewReader(json))
    18  
    19  		require.Nil(t, err)
    20  		require.Equal(t, msg.Platform, result.Platform, "ids do not match")
    21  	})
    22  
    23  	t.Run("should throw an error when the message is nil", func(t *testing.T) {
    24  		_, err := PushNotificationFromJson(nil)
    25  		require.NotNil(t, err)
    26  		require.Equal(t, "push notification data can't be nil", err.Error())
    27  	})
    28  
    29  	t.Run("should throw an error when the message parsing fails", func(t *testing.T) {
    30  		_, err := PushNotificationFromJson(strings.NewReader(""))
    31  		require.NotNil(t, err)
    32  		require.Equal(t, "EOF", err.Error())
    33  	})
    34  }
    35  
    36  func TestPushNotificationAck(t *testing.T) {
    37  	t.Run("should build a push notification ack from JSON", func(t *testing.T) {
    38  		msg := PushNotificationAck{ClientPlatform: "test"}
    39  		json := msg.ToJson()
    40  		result, err := PushNotificationAckFromJson(strings.NewReader(json))
    41  
    42  		require.Nil(t, err)
    43  		require.Equal(t, msg.ClientPlatform, result.ClientPlatform, "ids do not match")
    44  	})
    45  
    46  	t.Run("should throw an error when the message is nil", func(t *testing.T) {
    47  		_, err := PushNotificationAckFromJson(nil)
    48  		require.NotNil(t, err)
    49  		require.Equal(t, "push notification data can't be nil", err.Error())
    50  	})
    51  
    52  	t.Run("should throw an error when the message parsing fails", func(t *testing.T) {
    53  		_, err := PushNotificationAckFromJson(strings.NewReader(""))
    54  		require.NotNil(t, err)
    55  		require.Equal(t, "EOF", err.Error())
    56  	})
    57  }
    58  
    59  func TestPushNotificationDeviceId(t *testing.T) {
    60  
    61  	msg := PushNotification{Platform: "test"}
    62  
    63  	msg.SetDeviceIdAndPlatform("android:12345")
    64  	require.Equal(t, msg.Platform, "android", msg.Platform)
    65  	require.Equal(t, msg.DeviceId, "12345", msg.DeviceId)
    66  	msg.Platform = ""
    67  	msg.DeviceId = ""
    68  
    69  	msg.SetDeviceIdAndPlatform("android:12:345")
    70  	require.Equal(t, msg.Platform, "android", msg.Platform)
    71  	require.Equal(t, msg.DeviceId, "12:345", msg.DeviceId)
    72  	msg.Platform = ""
    73  	msg.DeviceId = ""
    74  
    75  	msg.SetDeviceIdAndPlatform("android::12345")
    76  	require.Equal(t, msg.Platform, "android", msg.Platform)
    77  	require.Equal(t, msg.DeviceId, ":12345", msg.DeviceId)
    78  	msg.Platform = ""
    79  	msg.DeviceId = ""
    80  
    81  	msg.SetDeviceIdAndPlatform(":12345")
    82  	require.Equal(t, msg.Platform, "", msg.Platform)
    83  	require.Equal(t, msg.DeviceId, "12345", msg.DeviceId)
    84  	msg.Platform = ""
    85  	msg.DeviceId = ""
    86  
    87  	msg.SetDeviceIdAndPlatform("android:")
    88  	require.Equal(t, msg.Platform, "android", msg.Platform)
    89  	require.Equal(t, msg.DeviceId, "", msg.DeviceId)
    90  	msg.Platform = ""
    91  	msg.DeviceId = ""
    92  
    93  	msg.SetDeviceIdAndPlatform("")
    94  	require.Equal(t, msg.Platform, "", msg.Platform)
    95  	require.Equal(t, msg.DeviceId, "", msg.DeviceId)
    96  	msg.Platform = ""
    97  	msg.DeviceId = ""
    98  
    99  	msg.SetDeviceIdAndPlatform(":")
   100  	require.Equal(t, msg.Platform, "", msg.Platform)
   101  	require.Equal(t, msg.DeviceId, "", msg.DeviceId)
   102  	msg.Platform = ""
   103  	msg.DeviceId = ""
   104  }