|
| 1 | +package handler |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/talesofai/okp/internal/config" |
| 9 | + "github.com/talesofai/okp/internal/model" |
| 10 | + "github.com/talesofai/okp/internal/testutil" |
| 11 | +) |
| 12 | + |
| 13 | +func TestSuccessfulKnowledgeReadsFeedDomainActivity(t *testing.T) { |
| 14 | + db := testutil.OpenDatabase(t) |
| 15 | + previousConfig := config.C |
| 16 | + config.C = config.Config{ExecutionGrantKey: "test-key"} |
| 17 | + t.Cleanup(func() { config.C = previousConfig }) |
| 18 | + |
| 19 | + for _, user := range []model.User{ |
| 20 | + {UUID: "reader", AuthType: "test", Role: "reader"}, |
| 21 | + {UUID: "outsider", AuthType: "test", Role: "reader"}, |
| 22 | + } { |
| 23 | + if err := db.Create(&user).Error; err != nil { |
| 24 | + t.Fatal(err) |
| 25 | + } |
| 26 | + } |
| 27 | + if err := db.Create(&[]model.DomainMeta{ |
| 28 | + {Domain: "public", Readme: "# Public", Visibility: "public"}, |
| 29 | + {Domain: "private", Readme: "# Private", Visibility: "private"}, |
| 30 | + }).Error; err != nil { |
| 31 | + t.Fatal(err) |
| 32 | + } |
| 33 | + if err := db.Create(&[]model.Concept{ |
| 34 | + {ID: "public/Note/one", Domain: "public", Type: "Note", Title: "One", Provenance: model.JSONMap{"source": "test", "agent": "test"}}, |
| 35 | + {ID: "private/Note/hidden", Domain: "private", Type: "Note", Title: "Hidden", Provenance: model.JSONMap{"source": "test", "agent": "test"}}, |
| 36 | + }).Error; err != nil { |
| 37 | + t.Fatal(err) |
| 38 | + } |
| 39 | + |
| 40 | + router := NewRouter() |
| 41 | + readerToken := testExecutionToken(t, "test-key", "reader") |
| 42 | + outsiderToken := testExecutionToken(t, "test-key", "outsider") |
| 43 | + |
| 44 | + assertStatus(t, testRequest(t, router, readerToken, http.MethodGet, "/api/v1/concepts?domain=public", nil), http.StatusOK) |
| 45 | + assertStatus(t, testRequest(t, router, readerToken, http.MethodGet, "/api/v1/concepts/public/Note/one", nil), http.StatusOK) |
| 46 | + assertStatus(t, testRequest(t, router, readerToken, http.MethodGet, "/api/v1/links/public/Note/one", nil), http.StatusOK) |
| 47 | + assertStatus(t, testRequest(t, router, readerToken, http.MethodGet, "/api/v1/domains/public", nil), http.StatusOK) |
| 48 | + assertStatus(t, testRequest(t, router, readerToken, http.MethodGet, "/api/v1/concepts?domain=public&q=absent", nil), http.StatusOK) |
| 49 | + |
| 50 | + // Hidden/private requests and the activity endpoint itself must not inflate reads. |
| 51 | + assertStatus(t, testRequest(t, router, outsiderToken, http.MethodGet, "/api/v1/concepts/private/Note/hidden", nil), http.StatusNotFound) |
| 52 | + assertStatus(t, testRequest(t, router, outsiderToken, http.MethodGet, "/api/v1/concepts?domain=private", nil), http.StatusOK) |
| 53 | + res := testRequest(t, router, readerToken, http.MethodGet, "/api/v1/domains/public/activity?days=1", nil) |
| 54 | + assertStatus(t, res, http.StatusOK) |
| 55 | + |
| 56 | + var activity struct { |
| 57 | + Points []struct { |
| 58 | + Reads int64 `json:"reads"` |
| 59 | + } `json:"points"` |
| 60 | + } |
| 61 | + decodeResponse(t, res, &activity) |
| 62 | + if len(activity.Points) != 1 || activity.Points[0].Reads != 5 { |
| 63 | + t.Fatalf("activity reads = %+v, want 5", activity.Points) |
| 64 | + } |
| 65 | + |
| 66 | + var privateReads int64 |
| 67 | + if err := db.Model(&model.DomainReadStat{}).Where("domain = ?", "private").Count(&privateReads).Error; err != nil { |
| 68 | + t.Fatal(err) |
| 69 | + } |
| 70 | + if privateReads != 0 { |
| 71 | + t.Fatalf("private reads should remain zero, got %d", privateReads) |
| 72 | + } |
| 73 | + |
| 74 | + var publicStat model.DomainReadStat |
| 75 | + if err := db.Where("domain = ? AND date = ?", "public", time.Now().UTC().Format("2006-01-02")).First(&publicStat).Error; err != nil { |
| 76 | + t.Fatal(err) |
| 77 | + } |
| 78 | + if publicStat.Reads != 5 { |
| 79 | + t.Fatalf("stored reads = %d, want 5", publicStat.Reads) |
| 80 | + } |
| 81 | +} |
0 commit comments