github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/model/client4_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  	"net/http"
     8  	"net/http/httptest"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  // https://github.com/mattermost/mattermost-server/v5/issues/8205
    16  func TestClient4CreatePost(t *testing.T) {
    17  	post := &Post{
    18  		Props: map[string]interface{}{
    19  			"attachments": []*SlackAttachment{
    20  				{
    21  					Actions: []*PostAction{
    22  						{
    23  							Integration: &PostActionIntegration{
    24  								Context: map[string]interface{}{
    25  									"foo": "bar",
    26  								},
    27  								URL: "http://foo.com",
    28  							},
    29  							Name: "Foo",
    30  						},
    31  					},
    32  				},
    33  			},
    34  		},
    35  	}
    36  
    37  	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    38  		attachments := PostFromJson(r.Body).Attachments()
    39  		assert.Equal(t, []*SlackAttachment{
    40  			{
    41  				Actions: []*PostAction{
    42  					{
    43  						Integration: &PostActionIntegration{
    44  							Context: map[string]interface{}{
    45  								"foo": "bar",
    46  							},
    47  							URL: "http://foo.com",
    48  						},
    49  						Name: "Foo",
    50  					},
    51  				},
    52  			},
    53  		}, attachments)
    54  	}))
    55  
    56  	client := NewAPIv4Client(server.URL)
    57  	_, resp := client.CreatePost(post)
    58  	assert.Equal(t, http.StatusOK, resp.StatusCode)
    59  }
    60  
    61  func TestClient4SetToken(t *testing.T) {
    62  	expected := NewId()
    63  
    64  	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    65  		authHeader := r.Header.Get(HEADER_AUTH)
    66  
    67  		token := strings.Split(authHeader, HEADER_BEARER)
    68  
    69  		if len(token) < 2 {
    70  			t.Errorf("wrong authorization header format, got %s, expected: %s %s", authHeader, HEADER_BEARER, expected)
    71  		}
    72  
    73  		assert.Equal(t, expected, strings.TrimSpace(token[1]))
    74  	}))
    75  
    76  	client := NewAPIv4Client(server.URL)
    77  	client.SetToken(expected)
    78  
    79  	_, resp := client.GetMe("")
    80  	assert.Equal(t, http.StatusOK, resp.StatusCode)
    81  }
    82  
    83  func TestClient4MockSession(t *testing.T) {
    84  	expected := NewId()
    85  
    86  	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    87  		authHeader := r.Header.Get(HEADER_AUTH)
    88  
    89  		token := strings.Split(authHeader, HEADER_BEARER)
    90  
    91  		if len(token) < 2 {
    92  			t.Errorf("wrong authorization header format, got %s, expected: %s %s", authHeader, HEADER_BEARER, expected)
    93  		}
    94  
    95  		assert.Equal(t, expected, strings.TrimSpace(token[1]))
    96  	}))
    97  
    98  	client := NewAPIv4Client(server.URL)
    99  	client.MockSession(expected)
   100  
   101  	_, resp := client.GetMe("")
   102  	assert.Equal(t, http.StatusOK, resp.StatusCode)
   103  }