Exemple #1
0
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.")
}
Exemple #2
0
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)
	}
}
Exemple #3
0
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))
	}
}
Exemple #4
0
func TestEndToEndIntegration(t *testing.T) {
	ts := httptest.NewServer(fakeServerHandler)
	defer ts.Close()

	// Override the sleep function
	feedwatcher.After = func(d time.Duration) <-chan time.Time {
		logrus.Infof("Call to mock After, waiting for just 1 second.")
		return time.After(time.Second * time.Duration(1))
	}

	cfg := config.NewTestConfig()
	d := commands.NewDaemon(cfg)
	err := d.DBH.Migrate(db.SchemaMigrations())
	if err != nil {
		t.Fatalf("Error loading fixture data: %v", err)
	}

	u, err := d.DBH.AddUser("user1", "foo@localhost", "foo")
	if err != nil {
		t.Fatalf("Error adding user: %v", err)
	}

	f, err := d.DBH.AddFeed("test feed", fmt.Sprintf("%s/feed1.atom", ts.URL))
	if err != nil {
		t.Fatalf("Error adding feed: %v", err)
	}

	err = d.DBH.AddFeedsToUser(u, []*db.FeedInfo{f})
	if err != nil {
		t.Fatalf("Error adding feed to user: %v", err)
	}

	allFeeds, err := d.DBH.GetAllFeeds()

	if err != nil {
		t.Fatalf("Error reading feeds: %s", err.Error())
	}

	d.CreateAndStartFeedWatchers(allFeeds[0:1])

	resp := <-d.RespChan
	if resp.Error != nil {
		t.Fatalf("Should not have gotten an error. got: %s", resp.Error)
	}
	if len(resp.Items) != 25 {
		t.Errorf("Expected 25 items from the feed. Got %d", len(resp.Items))
	}

	resp = <-d.RespChan
	if resp.Error != nil {
		t.Fatalf("Should not have gotten an error. got: %s", resp.Error)
	}
	if len(resp.Items) != 0 {
		t.Errorf("Expected 0 items from the feed. Got %d", len(resp.Items))
	}
}
Exemple #5
0
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.")
	}
}
Exemple #6
0
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)
	}
}
Exemple #7
0
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")
	}
}
Exemple #8
0
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)
	}
}
Exemple #9
0
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)
	}
}
Exemple #10
0
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)
	}
}
Exemple #11
0
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.")
	}
}
Exemple #12
0
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)
	}
}
Exemple #13
0
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)
	}
}