func TestAddFeedWithUsers(t *testing.T) { cfg := config.NewTestConfig() fcmd := feedCommand{ Config: cfg, DBH: db.NewMemoryDBHandle(NullLogger(), true), } testFeedURL := "http://testfeedurl" testUserEmail := "*****@*****.**" fcmd.FeedName = "testfeedname" fcmd.FeedURL = testFeedURL fcmd.UserEmails = []string{testUserEmail} err := fcmd.add() if err != nil { t.Fatalf("Error adding feed: %v", err) } _, err = fcmd.DBH.GetFeedByURL(testFeedURL) if err != nil { t.Errorf("Couldn't find feed with URL '%s', AddFeed didn't add it.", testFeedURL) } users, err := fcmd.DBH.GetFeedUsers(testFeedURL) for _, user := range users { if user.Email == testUserEmail { return } } t.Error("AddFeed didn't subscribe given users to feed.") }
// NewDaemon returns a pointer to a new Daemon struct with defaults set. func NewDaemon(cfg *config.Config) *Daemon { var dbh *db.Handle logger := logrus.New() log.SetupLogger(logger) if *debugdb { logger.Level = logrus.DebugLevel } if cfg.DB.Type == "memory" { dbh = db.NewMemoryDBHandle(logger, false) } else { dbh = db.NewDBHandle(cfg.DB.Path, logger) } cc := make(chan *feedwatcher.FeedCrawlRequest, 1) rc := make(chan *feedwatcher.FeedCrawlResponse) mc := mail.CreateAndStartMailer(cfg).OutgoingMail return &Daemon{ Config: cfg, CrawlChan: cc, RespChan: rc, MailChan: mc, DBH: dbh, Feeds: make(map[string]*feedwatcher.FeedWatcher), PollFeeds: true, Logger: logger, } }
func TestConfigUpdater(t *testing.T) { cfg := config.NewTestConfig() d := NewDaemon(cfg) d.Logger = NullLogger() d.DBH = db.NewMemoryDBHandle(NullLogger(), true) // Load Fixtures f, err := d.DBH.GetFeedByURL("http://localhost/feed1.atom") if err != nil { t.Fatalf("Error getting feed from db: %v", err) } d.Feeds["http://test/url"] = feedwatcher.NewFeedWatcher( *f, d.CrawlChan, d.RespChan, d.MailChan, d.DBH, []string{}, 300, 600, ) d.feedDbUpdate() if len(d.Feeds) != 3 { t.Errorf("Expected three feed entries after updater runs, got %d", len(d.Feeds)) } if _, ok := d.Feeds[f.URL]; !ok { t.Errorf("Expected %s in feed map.", f.URL) } }
func TestAddUserWithFeeds(t *testing.T) { ucmd := userCommand{ Config: config.NewTestConfig(), DBH: db.NewMemoryDBHandle(NullLogger(), false), Name: "test", Email: "*****@*****.**", Pass: "******", Feeds: []string{"http://test"}, } _, err := ucmd.DBH.AddFeed("testfeed", "http://test") if err != nil { t.Fatalf("Error adding feed: %s", err) } err = ucmd.add() if err != nil { t.Fatalf("Error adding user: %v", err) } dbUser, err := ucmd.DBH.GetUser("test") if err != nil { t.Errorf("Couldn't find user 'test', AddUser didn't add it.") } fi, err := ucmd.DBH.GetUsersFeeds(dbUser) if err != nil { t.Fatalf("Error getting feeds for user: %s", err) } if len(fi) != 1 { t.Errorf("Expected to get 1 feed, got %d", len(fi)) } }
func TestRemoveUnknownUser(t *testing.T) { ucmd := userCommand{ Config: config.NewTestConfig(), DBH: db.NewMemoryDBHandle(NullLogger(), false), Email: "*****@*****.**", } err := ucmd.delete() if err == nil { t.Fatal("Expected error when deleting unknown user, got none.") } }
func TestCreateDBCommand(t *testing.T) { cfg := config.NewTestConfig() cdbcmd := createDBCommand{ Config: cfg, DBH: db.NewMemoryDBHandle(NullLogger(), true), } err := cdbcmd.migrate() if err != nil { t.Fatalf("Error running createdb command: %v", err) } }
func TestAddUserWithInvalidEmailFormat(t *testing.T) { ucmd := userCommand{ Config: config.NewTestConfig(), DBH: db.NewMemoryDBHandle(NullLogger(), false), Name: "test", Email: "*****@*****.**", Pass: "", Feeds: []string{"http://test"}, } err := ucmd.add() if err == nil { t.Fatalf("Add user should have thrown and error when adding an invalid email") } }
func TestRemoveUser(t *testing.T) { ucmd := userCommand{ Config: config.NewTestConfig(), DBH: db.NewMemoryDBHandle(NullLogger(), false), Email: "*****@*****.**", } _, err := ucmd.DBH.AddUser("test", ucmd.Email, "pass") if err != nil { t.Fatalf("Error creating user: %s", err) } err = ucmd.delete() if err != nil { t.Fatalf("Error deleting user: %s", err) } }
func TestListUsers(t *testing.T) { cfg := config.NewTestConfig() ucmd := userCommand{ Config: cfg, DBH: db.NewMemoryDBHandle(NullLogger(), false), } _, err := ucmd.DBH.AddUser("test", "*****@*****.**", "pass") if err != nil { t.Fatalf("Error adding user to db: %s", err) } err = ucmd.list() if err != nil { t.Fatalf("Error listing users: %v", err) } }
func TestUnsubscribeUserToUnknownFeed(t *testing.T) { ucmd := userCommand{ Config: config.NewTestConfig(), DBH: db.NewMemoryDBHandle(NullLogger(), false), Email: "*****@*****.**", Feeds: []string{"https://testing/feed.atom"}, } _, err := ucmd.DBH.AddUser("test", ucmd.Email, "pass") if err != nil { t.Fatalf("Error creating user: %s", err) } err = ucmd.unsubscribe() if err != nil { t.Fatalf("Error when unsubscribing user from unknwon feed: %v.", err) } }
func TestFeedWithBadEntity(t *testing.T) { d := db.NewMemoryDBHandle(NullLogger(), true) feeds, err := d.GetAllFeeds() if err != nil { t.Fatalf("Error getting all feeds: %v", err) } u := feeds[0] feedResp, err := ioutil.ReadFile("../testdata/bad_entity.rss") if err != nil { t.Fatal("Error reading test feed.") } _, _, err = ParseFeed(u.URL, feedResp) if err != nil { t.Fatal("Feed should be able to parse feeds with unescaped entities") } }
func TestAddUser(t *testing.T) { ucmd := userCommand{ Config: config.NewTestConfig(), DBH: db.NewMemoryDBHandle(NullLogger(), false), Name: "test", Email: "*****@*****.**", Pass: "******", } err := ucmd.add() if err != nil { t.Fatalf("Error adding user: %v", err) } _, err = ucmd.DBH.GetUser("test") if err != nil { t.Errorf("Couldn't find user 'test', AddUser didn't add it.") } }
func SetupTest(t *testing.T, feedPath string) (*FeedWatcher, []byte, *mail.Dispatcher) { log.SetNullOutput() crawlChan := make(chan *FeedCrawlRequest) responseChan := make(chan *FeedCrawlResponse) mailDispatcher := mail.CreateAndStartStubMailer() d := db.NewMemoryDBHandle(NullLogger(), true) feeds, err := d.GetAllFeeds() if err != nil { t.Fatalf("Error getting feeds: %v", err) } if len(feeds) < 1 { t.Fatalf("Error: no feeds returned from database") } feedResp, err := ioutil.ReadFile(feedPath) if err != nil { t.Fatal("Error reading test feed.") } return NewFeedWatcher(*feeds[0], crawlChan, responseChan, mailDispatcher.OutgoingMail, d, []string{}, 30, 100), feedResp, mailDispatcher }
func TestSubscribeUser(t *testing.T) { ucmd := userCommand{ Config: config.NewTestConfig(), DBH: db.NewMemoryDBHandle(NullLogger(), false), Email: "*****@*****.**", Feeds: []string{"https://testing/feed.atom"}, } _, err := ucmd.DBH.AddUser("test", ucmd.Email, "pass") if err != nil { t.Fatalf("Error creating user: %s", err) } _, err = ucmd.DBH.AddFeed("testing", ucmd.Feeds[0]) if err != nil { t.Fatalf("Error creating feed: %s", err) } err = ucmd.subscribe() if err != nil { t.Fatalf("Error subscribing user to feed: %v", err) } }
func TestSendToUsers(t *testing.T) { dbh := db.NewMemoryDBHandle(NullLogger(), true) users, err := dbh.GetAllUsers() if err != nil { t.Fatalf("Error getting users: %v", err) } feeds, err := dbh.GetAllFeeds() if err != nil { t.Fatalf("Error getting feeds: %v", err) } mm := &MockedMailer{} md := NewDispatcher( "*****@*****.**", mm, ) f := &feed.Feed{ URL: feeds[0].URL, } s := &feed.Story{ Feed: f, } mr := Request{ Item: s, Addresses: []mail.Address{ {Address: users[0].Email}, {Address: users[1].Email}, }, } err = md.handleMailRequest(&mr) if err != nil { t.Fatalf("Error sending to users: %s", err) } if mm.Called != 2 { t.Fatalf("Expected 2 calls to the mailer, got %d", mm.Called) } }
func TestAddFeed(t *testing.T) { cfg := config.NewTestConfig() fcmd := feedCommand{ Config: cfg, DBH: db.NewMemoryDBHandle(NullLogger(), true), } testFeedURL := "http://testfeedurl" fcmd.FeedName = "testfeedname" fcmd.FeedURL = testFeedURL fcmd.UserEmails = []string{} err := fcmd.add() if err != nil { t.Fatalf("Error adding feed: %v", err) } _, err = fcmd.DBH.GetFeedByURL(testFeedURL) if err != nil { t.Errorf("Couldn't find feed with URL '%s', AddFeed didn't add it.", testFeedURL) } }