eintopf.info@v0.13.16/service/passwordrec/transport_test.go (about)

     1  // Copyright (C) 2024 The Eintopf authors
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    15  
    16  package passwordrec_test
    17  
    18  import (
    19  	"context"
    20  	"encoding/json"
    21  	"testing"
    22  
    23  	"eintopf.info"
    24  	"eintopf.info/internal/xhttptest"
    25  	"eintopf.info/service/passwordrec"
    26  	"eintopf.info/service/user"
    27  	"eintopf.info/test"
    28  )
    29  
    30  func TestTransport(t *testing.T) {
    31  	emailService := test.NewMailer()
    32  	userService := user.NewMemoryStore()
    33  	service := passwordrec.NewService(emailService, userService, eintopf.NewTemplateEngine([]string{"testdata/themes/foo"}, nil, nil), passwordrec.NewMemoryStore(), "http://localhost")
    34  
    35  	_, err := userService.Create(context.Background(), &user.NewUser{
    36  		Email: "foo@example.com",
    37  	})
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  
    42  	tests := []xhttptest.HttpTest{
    43  		{
    44  			Name:       "RequestWithExistingEmail",
    45  			URI:        "/requestRecovery",
    46  			Method:     "POST",
    47  			Body:       mustMarshal(&passwordrec.PasswordRecovery{Email: "foo@example.com"}),
    48  			WantStatus: 200,
    49  		},
    50  		{
    51  			Name:       "RequestWithNonExistingEmailReturns200",
    52  			URI:        "/requestRecovery",
    53  			Method:     "POST",
    54  			Body:       mustMarshal(&passwordrec.PasswordRecovery{Email: "bar@example.com"}),
    55  			WantStatus: 200,
    56  		},
    57  	}
    58  
    59  	xhttptest.TestRouter(t, passwordrec.Router(service), tests)
    60  }
    61  
    62  func mustMarshal(v interface{}) string {
    63  	data, err := json.Marshal(v)
    64  	if err != nil {
    65  		panic("mustMarshal failed")
    66  	}
    67  	return string(data)
    68  }