package api import ( "bytes" "compress/gzip" "fmt" "io" "io/ioutil" "math/rand" "mime/multipart" "net/http" "net/http/httptest" "os" "strings" "testing" "time" "github.com/coreos/bbolt" "github.com/go-chi/chi" "github.com/go-pkgz/auth" "github.com/go-pkgz/auth/token" "github.com/go-pkgz/rest/cache" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/umputun/remark/backend/app/migrator" adminstore "github.com/umputun/remark/backend/app/store/admin" "github.com/umputun/remark/backend/app/store/engine" "github.com/umputun/remark/backend/app/store/service" ) func TestMigrator_Import(t *testing.T) { ts, teardown := prepImportSrv(t) defer teardown() r := strings.NewReader(`{"version":1} {"id":"2aa0478c-df1b-46b1-b561-03d507cf482c","pid":"","text":"

test test #1

","user":{"name":"developer one","id":"dev","picture":"/api/v1/avatar/remark.image","profile":"https://remark42.com","admin":true,"ip":"ae12fe3b5f129b5cc4cdd2b136b7b7947c4d2741"},"locator":{"site":"radio-t","url":"https://radio-t.com/blah1"},"score":0,"votes":{},"time":"2018-04-30T01:37:00.849053725-05:00"} {"id":"83fd97fd-ff64-48d1-9fb7-ca7769c77037","pid":"p1","text":"

test test #2

","user":{"name":"developer one","id":"dev","picture":"/api/v1/avatar/remark.image","profile":"https://remark42.com","admin":true,"ip":"ae12fe3b5f129b5cc4cdd2b136b7b7947c4d2741"},"locator":{"site":"radio-t","url":"https://radio-t.com/blah2"},"score":0,"votes":{},"time":"2018-04-30T01:37:00.861387771-05:00"}`) client := &http.Client{Timeout: 1 * time.Second} req, err := http.NewRequest("POST", ts.URL+"/import?site=radio-t&provider=native", r) req.SetBasicAuth("admin", "password") assert.Nil(t, err) resp, err := client.Do(req) assert.Nil(t, err) assert.Equal(t, http.StatusAccepted, resp.StatusCode) b, err := ioutil.ReadAll(resp.Body) assert.Nil(t, err) assert.Equal(t, "{\"status\":\"import request accepted\"}\n", string(b)) waitForImportCompletion(t, ts) } func TestMigrator_ImportForm(t *testing.T) { ts, teardown := prepImportSrv(t) defer teardown() r := strings.NewReader(`{"version":1} {"id":"2aa0478c-df1b-46b1-b561-03d507cf482c","pid":"","text":"

test test #1

","user":{"name":"developer one","id":"dev","picture":"/api/v1/avatar/remark.image","profile":"https://remark42.com","admin":true,"ip":"ae12fe3b5f129b5cc4cdd2b136b7b7947c4d2741"},"locator":{"site":"radio-t","url":"https://radio-t.com/blah1"},"score":0,"votes":{},"time":"2018-04-30T01:37:00.849053725-05:00"} {"id":"83fd97fd-ff64-48d1-9fb7-ca7769c77037","pid":"p1","text":"

test test #2

","user":{"name":"developer one","id":"dev","picture":"/api/v1/avatar/remark.image","profile":"https://remark42.com","admin":true,"ip":"ae12fe3b5f129b5cc4cdd2b136b7b7947c4d2741"},"locator":{"site":"radio-t","url":"https://radio-t.com/blah2"},"score":0,"votes":{},"time":"2018-04-30T01:37:00.861387771-05:00"}`) bodyBuf := &bytes.Buffer{} bodyWriter := multipart.NewWriter(bodyBuf) fileWriter, err := bodyWriter.CreateFormFile("file", "import.json") require.NoError(t, err) _, err = io.Copy(fileWriter, r) require.NoError(t, err) contentType := bodyWriter.FormDataContentType() require.NoError(t, bodyWriter.Close()) authts := strings.Replace(ts.URL, "http://", "http://admin:password@", 1) resp, err := http.Post(authts+"/import/form?site=radio-t&provider=native", contentType, bodyBuf) assert.Nil(t, err) assert.Equal(t, http.StatusAccepted, resp.StatusCode) b, err := ioutil.ReadAll(resp.Body) assert.Nil(t, err) assert.Equal(t, "{\"status\":\"import request accepted\"}\n", string(b)) waitForImportCompletion(t, ts) } func TestMigrator_ImportFromWP(t *testing.T) { ts, teardown := prepImportSrv(t) defer teardown() r := strings.NewReader(strings.Replace(xmlTestWP, "'", "`", -1)) client := &http.Client{Timeout: 1 * time.Second} req, err := http.NewRequest("POST", ts.URL+"/import?site=radio-t&provider=wordpress", r) assert.Nil(t, err) req.Header.Add("Content-Type", "application/xml; charset=utf-8") req.SetBasicAuth("admin", "password") resp, err := client.Do(req) assert.Nil(t, err) assert.Equal(t, http.StatusAccepted, resp.StatusCode) b, err := ioutil.ReadAll(resp.Body) assert.Nil(t, err) assert.Equal(t, "{\"status\":\"import request accepted\"}\n", string(b)) waitForImportCompletion(t, ts) } func TestMigrator_ImportRejected(t *testing.T) { ts, teardown := prepImportSrv(t) defer teardown() r := strings.NewReader(`{"version":1} {"id":"2aa0478c-df1b-46b1-b561-03d507cf482c","pid":"","text":"

test test #1

","user":{"name":"developer one","id":"dev","picture":"/api/v1/avatar/remark.image","profile":"https://remark42.com","admin":true,"ip":"ae12fe3b5f129b5cc4cdd2b136b7b7947c4d2741"},"locator":{"site":"radio-t","url":"https://radio-t.com/blah1"},"score":0,"votes":{},"time":"2018-04-30T01:37:00.849053725-05:00"} {"id":"83fd97fd-ff64-48d1-9fb7-ca7769c77037","pid":"p1","text":"

test test #2

","user":{"name":"developer one","id":"dev","picture":"/api/v1/avatar/remark.image","profile":"https://remark42.com","admin":true,"ip":"ae12fe3b5f129b5cc4cdd2b136b7b7947c4d2741"},"locator":{"site":"radio-t","url":"https://radio-t.com/blah2"},"score":0,"votes":{},"time":"2018-04-30T01:37:00.861387771-05:00"}`) client := &http.Client{Timeout: 1 * time.Second} req, err := http.NewRequest("POST", ts.URL+"/import?site=radio-t&provider=native&secret=XYZ", r) assert.Nil(t, err) resp, err := client.Do(req) assert.Nil(t, err) assert.Equal(t, http.StatusUnauthorized, resp.StatusCode) } func TestMigrator_ImportDouble(t *testing.T) { ts, teardown := prepImportSrv(t) defer teardown() tmpl := `{"id":"%d","pid":"","text":"

test test #1

","user":{"name":"developer one","id":"dev","picture":"/api/v1/avatar/remark.image","profile":"https://remark42.com","admin":true,"ip":"ae12fe3b5f129b5cc4cdd2b136b7b7947c4d2741"},"locator":{"site":"radio-t","url":"https://radio-t.com/blah1"},"score":0,"votes":{},"time":"2018-04-30T01:37:00.849053725-05:00"}` recs := []string{} for i := 0; i < 150; i++ { recs = append(recs, fmt.Sprintf(tmpl, i)) } r := strings.NewReader(`{"version":1}` + strings.Join(recs, "\n")) // reader with 10k records client := &http.Client{Timeout: 1 * time.Second} req, err := http.NewRequest("POST", ts.URL+"/import?site=radio-t&provider=native", r) req.SetBasicAuth("admin", "password") assert.Nil(t, err) resp, err := client.Do(req) assert.Nil(t, err) assert.Equal(t, http.StatusAccepted, resp.StatusCode) client = &http.Client{Timeout: 1 * time.Second} req, err = http.NewRequest("POST", ts.URL+"/import?site=radio-t&provider=native", r) req.SetBasicAuth("admin", "password") assert.Nil(t, err) resp, err = client.Do(req) assert.Nil(t, err) assert.Equal(t, http.StatusConflict, resp.StatusCode) waitForImportCompletion(t, ts) } func TestMigrator_ImportWaitExpired(t *testing.T) { ts, teardown := prepImportSrv(t) defer teardown() tmpl := `{"id":"%d","pid":"","text":"

test test #1

","user":{"name":"developer one","id":"dev","picture":"/api/v1/avatar/remark.image","profile":"https://remark42.com","admin":true,"ip":"ae12fe3b5f129b5cc4cdd2b136b7b7947c4d2741"},"locator":{"site":"radio-t","url":"https://radio-t.com/blah1"},"score":0,"votes":{},"time":"2018-04-30T01:37:00.849053725-05:00"}` recs := []string{} for i := 0; i < 150; i++ { recs = append(recs, fmt.Sprintf(tmpl, i)) } r := strings.NewReader(`{"version":1}` + strings.Join(recs, "\n")) // reader with 10k records client := &http.Client{Timeout: 1 * time.Second} req, err := http.NewRequest("POST", ts.URL+"/import?site=radio-t&provider=native", r) req.SetBasicAuth("admin", "password") require.Nil(t, err) resp, err := client.Do(req) assert.Nil(t, err) assert.Equal(t, http.StatusAccepted, resp.StatusCode) client = &http.Client{Timeout: 10 * time.Second} req, err = http.NewRequest("GET", ts.URL+"/import/wait?site=radio-t&timeout=100ms", nil) req.SetBasicAuth("admin", "password") assert.NoError(t, err) resp, err = client.Do(req) assert.NoError(t, err) assert.Equal(t, http.StatusGatewayTimeout, resp.StatusCode) waitForImportCompletion(t, ts) } func TestMigrator_Export(t *testing.T) { ts, teardown := prepImportSrv(t) defer teardown() r := strings.NewReader(`{"version":1} {"id":"2aa0478c-df1b-46b1-b561-03d507cf482c","pid":"","text":"

test test #1

","user":{"name":"developer one","id":"dev","picture":"/api/v1/avatar/remark.image","profile":"https://remark42.com","admin":true,"ip":"ae12fe3b5f129b5cc4cdd2b136b7b7947c4d2741"},"locator":{"site":"radio-t","url":"https://radio-t.com/blah1"},"score":0,"votes":{},"time":"2018-04-30T01:37:00.849053725-05:00"} {"id":"83fd97fd-ff64-48d1-9fb7-ca7769c77037","pid":"p1","text":"

test test #2

","user":{"name":"developer one","id":"dev","picture":"/api/v1/avatar/remark.image","profile":"https://remark42.com","admin":true,"ip":"ae12fe3b5f129b5cc4cdd2b136b7b7947c4d2741"},"locator":{"site":"radio-t","url":"https://radio-t.com/blah2"},"score":0,"votes":{},"time":"2018-04-30T01:37:00.861387771-05:00"}`) // import comments first client := &http.Client{Timeout: 1 * time.Second} req, err := http.NewRequest("POST", ts.URL+"/import?site=radio-t&provider=native", r) require.Nil(t, err) req.SetBasicAuth("admin", "password") resp, err := client.Do(req) require.Nil(t, err) require.Equal(t, http.StatusAccepted, resp.StatusCode) waitForImportCompletion(t, ts) // check file mode req, err = http.NewRequest("GET", ts.URL+"/export?mode=file&site=radio-t", nil) require.Nil(t, err) req.SetBasicAuth("admin", "password") resp, err = client.Do(req) require.Nil(t, err) require.Equal(t, 200, resp.StatusCode) require.Equal(t, "application/gzip", resp.Header.Get("Content-Type")) ungzReader, err := gzip.NewReader(resp.Body) assert.NoError(t, err) ungzBody, err := ioutil.ReadAll(ungzReader) assert.NoError(t, err) assert.Equal(t, 3, strings.Count(string(ungzBody), "\n")) assert.Equal(t, 2, strings.Count(string(ungzBody), "\"text\"")) t.Logf("%s", string(ungzBody)) // check stream mode req, err = http.NewRequest("GET", ts.URL+"/export?mode=stream&site=radio-t", nil) require.Nil(t, err) req.SetBasicAuth("admin", "password") resp, err = client.Do(req) require.Nil(t, err) require.Equal(t, 200, resp.StatusCode) require.Equal(t, "text/plain; charset=utf-8", resp.Header.Get("Content-Type")) body, err := ioutil.ReadAll(resp.Body) assert.NoError(t, err) assert.Equal(t, 3, strings.Count(string(body), "\n")) assert.Equal(t, 2, strings.Count(string(body), "\"text\"")) t.Logf("%s", string(body)) req, err = http.NewRequest("GET", ts.URL+"/export?site=radio-t", nil) require.Nil(t, err) resp, err = client.Do(req) require.Nil(t, err) require.Equal(t, http.StatusUnauthorized, resp.StatusCode) } func waitForImportCompletion(t *testing.T, ts *httptest.Server) { client := &http.Client{Timeout: 10 * time.Second} req, err := http.NewRequest("GET", ts.URL+"/import/wait?site=radio-t", nil) req.SetBasicAuth("admin", "password") assert.NoError(t, err) resp, err := client.Do(req) require.NoError(t, err) assert.Equal(t, 200, resp.StatusCode) b, err := ioutil.ReadAll(resp.Body) require.NoError(t, err) defer resp.Body.Close() assert.Equal(t, "{\"site_id\":\"radio-t\",\"status\":\"completed\"}\n", string(b)) } func prepImportSrv(t *testing.T) (ts *httptest.Server, teardown func()) { testDb := fmt.Sprintf("/tmp/test-remark-import-%d.db", rand.Int31()) b, err := engine.NewBoltDB(bolt.Options{}, engine.BoltSite{FileName: testDb, SiteID: "radio-t"}) require.Nil(t, err) adminStore := adminstore.NewStaticStore("123456", []string{"a1", "a2"}, "admin@remark-42.com") dataStore := &service.DataStore{Interface: b, AdminStore: adminStore} svc := &Migrator{ DisqusImporter: &migrator.Disqus{DataStore: dataStore}, WordPressImporter: &migrator.WordPress{DataStore: dataStore}, NativeImporter: &migrator.Native{DataStore: dataStore}, NativeExporter: &migrator.Native{DataStore: dataStore}, Cache: &cache.Nop{}, KeyStore: adminStore, } a := auth.NewService(auth.Opts{ AdminPasswd: "password", SecretReader: token.SecretFunc(func() (string, error) { return "123456", nil }), Issuer: "test", }) am := a.Middleware() routes := svc.withRoutes(chi.NewRouter().With(am.Auth).With(am.AdminOnly)) ts = httptest.NewServer(routes) teardown = func() { ts.Close() require.NoError(t, b.Close()) _ = os.Remove(testDb) } return ts, teardown } var xmlTestWP = ` Real Men Wear Dress.es https://realmenweardress.es SuperAdmin's gaming and technological musings Mon, 23 Jul 2018 10:21:47 +0000 en-US 1.2 https://realmenweardress.es https://realmenweardress.es 2 1 25 39 https://wordpress.org/?v=4.8.1 Post without comments https://realmenweardress.es/2010/06/hello-world/screenshot_013110_200413/ Sat, 19 Jun 2010 08:34:13 +0000 http://realmenweardress.es/wp-content/uploads/2010/06/ScreenShot_013110_200413.jpeg 6 1 0 0 Post with comments. One is not approved https://realmenweardress.es/2010/07/do-you-rp/ Mon, 19 Jul 2010 14:24:22 +0000 http://realmenweardress.es/?p=100 I need to stand on things else I can't reachMeet Grokknomel?]]> 100 0 0 0 8 http://superuser1.blogspot.com 0 0 9 http://thewowstorm.wordpress.com 0 0 13 http://cynwise.wordpress.com/2010/07/21/wednesday-reading-8/ 0 0 14 http://realmenweardress.es 13 2 `