github.com/jancarloviray/community@v0.41.1-0.20170124221257-33a66c87cf2f/core/api/mail/mailer_test.go (about)

     1  // Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
     2  //
     3  // This software (Documize Community Edition) is licensed under
     4  // GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
     5  //
     6  // You can operate outside the AGPL restrictions by purchasing
     7  // Documize Enterprise Edition and obtaining a commercial license
     8  // by contacting <sales@documize.com>.
     9  //
    10  // https://documize.com
    11  
    12  package mail
    13  
    14  import (
    15  	//"fmt"
    16  	"errors"
    17  	"net/smtp"
    18  	"os"
    19  	"strings"
    20  	"testing"
    21  
    22  	"github.com/documize/community/core/log"
    23  )
    24  
    25  func TestMail(t *testing.T) {
    26  	sender := "sender@documize.com"
    27  	recipient := "recipient@documize.com"
    28  	contains := []string{}
    29  	var returnError error
    30  	smtpSendMail = func(addr string, a smtp.Auth, from string, to []string, msg []byte) error {
    31  		if addr != getHost() {
    32  			t.Error("host incorrect:" + addr)
    33  		}
    34  		if from != "noreply@documize.com" && from != "hello@documize.com" {
    35  			t.Error("sender incorrect:" + from)
    36  		}
    37  		if len(to) == 0 {
    38  			t.Error("no recipient")
    39  		} else {
    40  			if to[0] != recipient {
    41  				t.Error("recipient incorrect:" + to[0])
    42  			}
    43  		}
    44  		for _, cont := range contains {
    45  			if !strings.Contains(string(msg), cont) {
    46  				t.Error("body does not contain:`" + cont + "` html:" + string(msg))
    47  			}
    48  		}
    49  		//fmt.Println("DEBUG testSendMail", addr, a, from, to)
    50  		return returnError
    51  	}
    52  
    53  	err := os.Chdir("..")
    54  	if err != nil {
    55  		t.Error(err)
    56  	}
    57  	url := "https://documize.com"
    58  	contains = []string{url, sender}
    59  	InviteNewUser(recipient, sender, url, "username", "password")
    60  	contains = []string{url, "Your colleague"}
    61  	InviteNewUser(recipient, "", url, "username", "password")
    62  	contains = []string{url, sender}
    63  	InviteExistingUser(recipient, sender, url)
    64  	contains = []string{url, "Your colleague"}
    65  	InviteExistingUser(recipient, "", url)
    66  	contains = []string{url}
    67  	PasswordReset(recipient, url)
    68  	contains = []string{url, sender, "folder", "intro"}
    69  	ShareFolderExistingUser(recipient, sender, url, "folder", "intro")
    70  	contains = []string{url, "Your colleague", "folder", "intro"}
    71  	ShareFolderExistingUser(recipient, "", url, "folder", "intro")
    72  	contains = []string{url, sender, "folder", "invitationMessage string"}
    73  	ShareFolderNewUser(recipient, sender, url, "folder", "invitationMessage string")
    74  	contains = []string{url, "Your colleague", "folder", "invitationMessage string"}
    75  	ShareFolderNewUser(recipient, "", url, "folder", "invitationMessage string")
    76  
    77  	contains = []string{url}
    78  	returnError = errors.New("test error")
    79  	log.TestIfErr = true
    80  	InviteNewUser(recipient, sender, url, "username", "password")
    81  	if log.TestIfErr {
    82  		t.Error("did not log an error when it should have")
    83  	}
    84  	log.TestIfErr = true
    85  	InviteExistingUser(recipient, sender, url)
    86  	if log.TestIfErr {
    87  		t.Error("did not log an error when it should have")
    88  	}
    89  	log.TestIfErr = true
    90  	PasswordReset(recipient, url)
    91  	if log.TestIfErr {
    92  		t.Error("did not log an error when it should have")
    93  	}
    94  	log.TestIfErr = true
    95  	ShareFolderExistingUser(recipient, sender, url, "folder", "intro")
    96  	if log.TestIfErr {
    97  		t.Error("did not log an error when it should have")
    98  	}
    99  	log.TestIfErr = true
   100  	ShareFolderNewUser(recipient, sender, url, "folder", "invitationMessage string")
   101  	if log.TestIfErr {
   102  		t.Error("did not log an error when it should have")
   103  	}
   104  }
   105  
   106  // TODO: no tests (yet) for smtp.go as this is akin to a vendored package