|
| 1 | +// Copyright 2021 The Gitea Authors. |
| 2 | +// All rights reserved. |
| 3 | +// Use of this source code is governed by a MIT-style |
| 4 | +// license that can be found in the LICENSE file. |
| 5 | + |
| 6 | +package imap |
| 7 | + |
| 8 | +import ( |
| 9 | + "fmt" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "code.gitea.io/gitea/modules/setting" |
| 13 | + "github.com/emersion/go-imap" |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | +) |
| 16 | + |
| 17 | +var logs string |
| 18 | + |
| 19 | +type testIMAPClient struct { |
| 20 | + t *testing.T |
| 21 | +} |
| 22 | + |
| 23 | +func TestIMAP(t *testing.T) { |
| 24 | + // test GetUnReadMails |
| 25 | + logs = "" |
| 26 | + mails, err := c.GetUnReadMails(setting.MailReciveService.ReciveBox, 100) |
| 27 | + assert.NoError(t, err) |
| 28 | + if !assert.Equal(t, 2, len(mails)) { |
| 29 | + return |
| 30 | + } |
| 31 | + assert. Equal( t, "login: [email protected] 123456\n"+ |
| 32 | + "Select: INBOX, false\n"+ |
| 33 | + "Search: [\\Seen]\n"+ |
| 34 | + "logout\n", logs) |
| 35 | + |
| 36 | + // TODO |
| 37 | + // test handleReciveEmail |
| 38 | + // c.Client.(*testIMAPClient).t = t |
| 39 | + |
| 40 | + // for _, mail := range mails { |
| 41 | + // logs = "" |
| 42 | + // if !assert.NoError(t, mail.LoadHeader([]string{"From", "To", "In-Reply-To", "References"})) { |
| 43 | + // return |
| 44 | + // } |
| 45 | + // if !assert.NoError(t, handleReciveEmail(mail)) { |
| 46 | + // return |
| 47 | + // } |
| 48 | + // assert.Equal(t, "", logs) |
| 49 | + // } |
| 50 | +} |
| 51 | + |
| 52 | +func (c *testIMAPClient) Login(username, password string) error { |
| 53 | + logs += "login: " + username + " " + password + "\n" |
| 54 | + return nil |
| 55 | +} |
| 56 | + |
| 57 | +func (c *testIMAPClient) Logout() error { |
| 58 | + logs += "logout\n" |
| 59 | + return nil |
| 60 | +} |
| 61 | + |
| 62 | +func (c *testIMAPClient) Select(name string, readOnly bool) (*imap.MailboxStatus, error) { |
| 63 | + logs += fmt.Sprintf("Select: %v, %v\n", name, readOnly) |
| 64 | + if name != "INBOX" { |
| 65 | + return nil, fmt.Errorf("not found mail box") |
| 66 | + } |
| 67 | + |
| 68 | + return nil, nil |
| 69 | +} |
| 70 | + |
| 71 | +func (c *testIMAPClient) Search(criteria *imap.SearchCriteria) (seqNums []uint32, err error) { |
| 72 | + logs += fmt.Sprintf("Search: %v\n", criteria.WithoutFlags) |
| 73 | + |
| 74 | + return []uint32{1, 2}, nil |
| 75 | +} |
| 76 | + |
| 77 | +func (c *testIMAPClient) Store(seqset *imap.SeqSet, item imap.StoreItem, value interface{}, ch chan *imap.Message) error { |
| 78 | + logs += "Store\n" |
| 79 | + return nil |
| 80 | +} |
| 81 | + |
| 82 | +func (c *testIMAPClient) Expunge(ch chan uint32) error { |
| 83 | + logs += "Expunge\n" |
| 84 | + return nil |
| 85 | +} |
| 86 | + |
| 87 | +var testMails []string |
| 88 | + |
| 89 | +func (c *testIMAPClient) Fetch(seqset *imap.SeqSet, items []imap.FetchItem, ch chan *imap.Message) error { |
| 90 | + defer close(ch) |
| 91 | + |
| 92 | + // logs += fmt.Sprintf("Fetch: %v, %v\n", seqset.String(), items) |
| 93 | + // if len(seqset.Set) != 1 { |
| 94 | + // return nil |
| 95 | + // } |
| 96 | + // if seqset.Set[0].Start < 1 || seqset.Set[0].Start > 2 { |
| 97 | + // return nil |
| 98 | + // } |
| 99 | + |
| 100 | + // if testMails == nil { |
| 101 | + // // load test data |
| 102 | + // testMails = make([]string, 2) |
| 103 | + // issue1 := models.AssertExistsAndLoadBean(c.t, &models.Issue{ID: 1}).(*models.Issue) |
| 104 | + // // comment2 := models.AssertExistsAndLoadBean(c.t, &models.Comment{ID: 2}).(*models.Comment) |
| 105 | + // user2 := models.AssertExistsAndLoadBean(c.t, &models.User{ID: 2}).(*models.User) |
| 106 | + |
| 107 | + // testMails[0] = "From: " + user2.Email + "\r\n" + |
| 108 | + |
| 109 | + // "Subject: Re: " + issue1.Title + "\r\n" + |
| 110 | + // "Date: Wed, 11 May 2016 14:31:59 +0000\r\n" + |
| 111 | + // "Message-ID: <0000000@localhost/>\r\n" + |
| 112 | + // "" + |
| 113 | + // "Content-Type: text/plain\r\n" + |
| 114 | + // "\r\n" + |
| 115 | + // "test reply\r\n" + |
| 116 | + // "----- origin mail ------\r\n" + |
| 117 | + // issue1.Content |
| 118 | + // } |
| 119 | + |
| 120 | + return nil |
| 121 | +} |
0 commit comments