github.com/status-im/status-go@v1.1.0/notifier/notifier_test.go (about)

     1  package notifier
     2  
     3  import (
     4  	"errors"
     5  	"log"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/suite"
    11  )
    12  
    13  type NotifierTestSuite struct {
    14  	suite.Suite
    15  	url string
    16  	n   *Notifier
    17  }
    18  
    19  func (s *NotifierTestSuite) SetupTest() {
    20  	s.url = "http://localhost:3000"
    21  	s.n = New(s.url)
    22  }
    23  
    24  var flagtests = []struct {
    25  	response string
    26  	err      error
    27  }{
    28  	{
    29  		response: `{"counts":1,"logs":[{"type":"failed-push","platform":"android","token":"1111111","message":"Hello world","error":"invalid registration token"}],"success":"ok"}`,
    30  		err:      errors.New("invalid registration token"),
    31  	},
    32  	{
    33  		response: `{"counts":1,"success":"ok"}`,
    34  		err:      nil,
    35  	},
    36  }
    37  
    38  func (s *NotifierTestSuite) TestSendNotifications() {
    39  	for _, tt := range flagtests {
    40  		var apiStub = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    41  			w.WriteHeader(http.StatusOK)
    42  			if _, err := w.Write([]byte(tt.response)); err != nil {
    43  				log.Println(err.Error())
    44  			}
    45  		}))
    46  		defer apiStub.Close()
    47  
    48  		s.n.url = apiStub.URL
    49  		notification := Notification{
    50  			Tokens:   []string{"t1", "t2"},
    51  			Platform: 2,
    52  			Message:  "hello world",
    53  		}
    54  
    55  		err := s.n.Send([]*Notification{&notification})
    56  		s.Equal(tt.err, err)
    57  	}
    58  }
    59  
    60  func TestNotifierTestSuite(t *testing.T) {
    61  	suite.Run(t, new(NotifierTestSuite))
    62  }