github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/model/reaction_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 TestReactionIsValid(t *testing.T) {
    14  	tests := []struct {
    15  		// reaction
    16  		reaction Reaction
    17  		// error message to print
    18  		errMsg string
    19  		// should there be an error
    20  		shouldErr bool
    21  	}{
    22  		{
    23  			reaction: Reaction{
    24  				UserId:    NewId(),
    25  				PostId:    NewId(),
    26  				EmojiName: "emoji",
    27  				CreateAt:  GetMillis(),
    28  			},
    29  			errMsg:    "",
    30  			shouldErr: false,
    31  		},
    32  		{
    33  			reaction: Reaction{
    34  				UserId:    "",
    35  				PostId:    NewId(),
    36  				EmojiName: "emoji",
    37  				CreateAt:  GetMillis(),
    38  			},
    39  			errMsg:    "user id should be invalid",
    40  			shouldErr: true,
    41  		},
    42  		{
    43  			reaction: Reaction{
    44  				UserId:    "1234garbage",
    45  				PostId:    NewId(),
    46  				EmojiName: "emoji",
    47  				CreateAt:  GetMillis(),
    48  			},
    49  			errMsg:    "user id should be invalid",
    50  			shouldErr: true,
    51  		},
    52  		{
    53  			reaction: Reaction{
    54  				UserId:    NewId(),
    55  				PostId:    "",
    56  				EmojiName: "emoji",
    57  				CreateAt:  GetMillis(),
    58  			},
    59  			errMsg:    "post id should be invalid",
    60  			shouldErr: true,
    61  		},
    62  		{
    63  			reaction: Reaction{
    64  				UserId:    NewId(),
    65  				PostId:    "1234garbage",
    66  				EmojiName: "emoji",
    67  				CreateAt:  GetMillis(),
    68  			},
    69  			errMsg:    "post id should be invalid",
    70  			shouldErr: true,
    71  		},
    72  		{
    73  			reaction: Reaction{
    74  				UserId:    NewId(),
    75  				PostId:    NewId(),
    76  				EmojiName: strings.Repeat("a", 64),
    77  				CreateAt:  GetMillis(),
    78  			},
    79  			errMsg:    "",
    80  			shouldErr: false,
    81  		},
    82  		{
    83  			reaction: Reaction{
    84  				UserId:    NewId(),
    85  				PostId:    NewId(),
    86  				EmojiName: "emoji-",
    87  				CreateAt:  GetMillis(),
    88  			},
    89  			errMsg:    "",
    90  			shouldErr: false,
    91  		},
    92  		{
    93  			reaction: Reaction{
    94  				UserId:    NewId(),
    95  				PostId:    NewId(),
    96  				EmojiName: "emoji_",
    97  				CreateAt:  GetMillis(),
    98  			},
    99  			errMsg:    "",
   100  			shouldErr: false,
   101  		},
   102  		{
   103  			reaction: Reaction{
   104  				UserId:    NewId(),
   105  				PostId:    NewId(),
   106  				EmojiName: "+1",
   107  				CreateAt:  GetMillis(),
   108  			},
   109  			errMsg:    "",
   110  			shouldErr: false,
   111  		},
   112  		{
   113  			reaction: Reaction{
   114  				UserId:    NewId(),
   115  				PostId:    NewId(),
   116  				EmojiName: "emoji:",
   117  				CreateAt:  GetMillis(),
   118  			},
   119  			errMsg:    "",
   120  			shouldErr: true,
   121  		},
   122  		{
   123  			reaction: Reaction{
   124  				UserId:    NewId(),
   125  				PostId:    NewId(),
   126  				EmojiName: "",
   127  				CreateAt:  GetMillis(),
   128  			},
   129  			errMsg:    "emoji name should be invalid",
   130  			shouldErr: true,
   131  		},
   132  		{
   133  			reaction: Reaction{
   134  				UserId:    NewId(),
   135  				PostId:    NewId(),
   136  				EmojiName: strings.Repeat("a", 65),
   137  				CreateAt:  GetMillis(),
   138  			},
   139  			errMsg:    "emoji name should be invalid",
   140  			shouldErr: true,
   141  		},
   142  		{
   143  			reaction: Reaction{
   144  				UserId:    NewId(),
   145  				PostId:    NewId(),
   146  				EmojiName: "emoji",
   147  				CreateAt:  0,
   148  			},
   149  			errMsg:    "create at should be invalid",
   150  			shouldErr: true,
   151  		},
   152  	}
   153  
   154  	for _, test := range tests {
   155  		err := test.reaction.IsValid()
   156  		if test.shouldErr {
   157  			// there should be an error here
   158  			require.NotNil(t, err, test.errMsg)
   159  		} else {
   160  			// err should be nil here
   161  			require.Nil(t, err, test.errMsg)
   162  		}
   163  	}
   164  }