github.com/mad-app/mattermost-server@v5.11.1+incompatible/store/storetest/command_webhook_store.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package storetest
     5  
     6  import (
     7  	"testing"
     8  
     9  	"net/http"
    10  
    11  	"github.com/mattermost/mattermost-server/model"
    12  	"github.com/mattermost/mattermost-server/store"
    13  )
    14  
    15  func TestCommandWebhookStore(t *testing.T, ss store.Store) {
    16  	t.Run("", func(t *testing.T) { testCommandWebhookStore(t, ss) })
    17  }
    18  
    19  func testCommandWebhookStore(t *testing.T, ss store.Store) {
    20  	cws := ss.CommandWebhook()
    21  
    22  	h1 := &model.CommandWebhook{}
    23  	h1.CommandId = model.NewId()
    24  	h1.UserId = model.NewId()
    25  	h1.ChannelId = model.NewId()
    26  	h1 = (<-cws.Save(h1)).Data.(*model.CommandWebhook)
    27  
    28  	if r1 := <-cws.Get(h1.Id); r1.Err != nil {
    29  		t.Fatal(r1.Err)
    30  	} else {
    31  		if *r1.Data.(*model.CommandWebhook) != *h1 {
    32  			t.Fatal("invalid returned webhook")
    33  		}
    34  	}
    35  
    36  	if err := (<-cws.Get("123")).Err; err.StatusCode != http.StatusNotFound {
    37  		t.Fatal("Should have set the status as not found for missing id")
    38  	}
    39  
    40  	h2 := &model.CommandWebhook{}
    41  	h2.CreateAt = model.GetMillis() - 2*model.COMMAND_WEBHOOK_LIFETIME
    42  	h2.CommandId = model.NewId()
    43  	h2.UserId = model.NewId()
    44  	h2.ChannelId = model.NewId()
    45  	h2 = (<-cws.Save(h2)).Data.(*model.CommandWebhook)
    46  
    47  	if err := (<-cws.Get(h2.Id)).Err; err == nil || err.StatusCode != http.StatusNotFound {
    48  		t.Fatal("Should have set the status as not found for expired webhook")
    49  	}
    50  
    51  	cws.Cleanup()
    52  
    53  	if err := (<-cws.Get(h1.Id)).Err; err != nil {
    54  		t.Fatal("Should have no error getting unexpired webhook")
    55  	}
    56  
    57  	if err := (<-cws.Get(h2.Id)).Err; err.StatusCode != http.StatusNotFound {
    58  		t.Fatal("Should have set the status as not found for expired webhook")
    59  	}
    60  
    61  	if err := (<-cws.TryUse(h1.Id, 1)).Err; err != nil {
    62  		t.Fatal("Should be able to use webhook once")
    63  	}
    64  
    65  	if err := (<-cws.TryUse(h1.Id, 1)).Err; err == nil || err.StatusCode != http.StatusBadRequest {
    66  		t.Fatal("Should be able to use webhook once")
    67  	}
    68  }