@@ -2,7 +2,9 @@ package coderd_test
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
5
6
"io"
7
+ "net/http"
6
8
"testing"
7
9
"time"
8
10
@@ -14,6 +16,7 @@ import (
14
16
"cdr.dev/slog/sloggers/slogtest"
15
17
"github.com/coder/coder/agent"
16
18
"github.com/coder/coder/coderd/coderdtest"
19
+ "github.com/coder/coder/coderd/rbac"
17
20
"github.com/coder/coder/codersdk"
18
21
"github.com/coder/coder/codersdk/agentsdk"
19
22
"github.com/coder/coder/provisioner/echo"
@@ -380,3 +383,213 @@ func TestTemplateInsights_BadRequest(t *testing.T) {
380
383
})
381
384
assert .Error (t , err , "want error for bad interval" )
382
385
}
386
+
387
+ func TestTemplateInsights_RBAC (t * testing.T ) {
388
+ t .Parallel ()
389
+
390
+ y , m , d := time .Now ().UTC ().Date ()
391
+ today := time .Date (y , m , d , 0 , 0 , 0 , 0 , time .UTC )
392
+
393
+ type test struct {
394
+ interval codersdk.InsightsReportInterval
395
+ withTemplate bool
396
+ }
397
+
398
+ tests := []test {
399
+ {codersdk .InsightsReportIntervalDay , true },
400
+ {codersdk .InsightsReportIntervalDay , false },
401
+ {"" , true },
402
+ {"" , false },
403
+ }
404
+
405
+ for _ , tt := range tests {
406
+ tt := tt
407
+
408
+ t .Run (fmt .Sprintf ("with interval=%q" , tt .interval ), func (t * testing.T ) {
409
+ t .Parallel ()
410
+
411
+ t .Run ("AsOwner" , func (t * testing.T ) {
412
+ t .Parallel ()
413
+
414
+ client := coderdtest .New (t , & coderdtest.Options {})
415
+ admin := coderdtest .CreateFirstUser (t , client )
416
+
417
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitShort )
418
+ defer cancel ()
419
+
420
+ var templateIDs []uuid.UUID
421
+ if tt .withTemplate {
422
+ version := coderdtest .CreateTemplateVersion (t , client , admin .OrganizationID , nil )
423
+ template := coderdtest .CreateTemplate (t , client , admin .OrganizationID , version .ID )
424
+ templateIDs = append (templateIDs , template .ID )
425
+ }
426
+
427
+ _ , err := client .TemplateInsights (ctx , codersdk.TemplateInsightsRequest {
428
+ StartTime : today .AddDate (0 , 0 , - 1 ),
429
+ EndTime : today ,
430
+ Interval : tt .interval ,
431
+ TemplateIDs : templateIDs ,
432
+ })
433
+ require .NoError (t , err )
434
+ })
435
+ t .Run ("AsTemplateAdmin" , func (t * testing.T ) {
436
+ t .Parallel ()
437
+
438
+ client := coderdtest .New (t , & coderdtest.Options {})
439
+ admin := coderdtest .CreateFirstUser (t , client )
440
+
441
+ templateAdmin , _ := coderdtest .CreateAnotherUser (t , client , admin .OrganizationID , rbac .RoleTemplateAdmin ())
442
+
443
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitShort )
444
+ defer cancel ()
445
+
446
+ var templateIDs []uuid.UUID
447
+ if tt .withTemplate {
448
+ version := coderdtest .CreateTemplateVersion (t , client , admin .OrganizationID , nil )
449
+ template := coderdtest .CreateTemplate (t , client , admin .OrganizationID , version .ID )
450
+ templateIDs = append (templateIDs , template .ID )
451
+ }
452
+
453
+ _ , err := templateAdmin .TemplateInsights (ctx , codersdk.TemplateInsightsRequest {
454
+ StartTime : today .AddDate (0 , 0 , - 1 ),
455
+ EndTime : today ,
456
+ Interval : tt .interval ,
457
+ TemplateIDs : templateIDs ,
458
+ })
459
+ require .NoError (t , err )
460
+ })
461
+ t .Run ("AsRegularUser" , func (t * testing.T ) {
462
+ t .Parallel ()
463
+
464
+ client := coderdtest .New (t , & coderdtest.Options {})
465
+ admin := coderdtest .CreateFirstUser (t , client )
466
+
467
+ regular , _ := coderdtest .CreateAnotherUser (t , client , admin .OrganizationID )
468
+
469
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitShort )
470
+ defer cancel ()
471
+
472
+ var templateIDs []uuid.UUID
473
+ if tt .withTemplate {
474
+ version := coderdtest .CreateTemplateVersion (t , client , admin .OrganizationID , nil )
475
+ template := coderdtest .CreateTemplate (t , client , admin .OrganizationID , version .ID )
476
+ templateIDs = append (templateIDs , template .ID )
477
+ }
478
+
479
+ _ , err := regular .TemplateInsights (ctx , codersdk.TemplateInsightsRequest {
480
+ StartTime : today .AddDate (0 , 0 , - 1 ),
481
+ EndTime : today ,
482
+ Interval : tt .interval ,
483
+ TemplateIDs : templateIDs ,
484
+ })
485
+ require .Error (t , err )
486
+ var apiErr * codersdk.Error
487
+ require .ErrorAs (t , err , & apiErr )
488
+ require .Equal (t , http .StatusNotFound , apiErr .StatusCode ())
489
+ })
490
+ })
491
+ }
492
+ }
493
+
494
+ func TestUserLatencyInsights_RBAC (t * testing.T ) {
495
+ t .Parallel ()
496
+
497
+ y , m , d := time .Now ().UTC ().Date ()
498
+ today := time .Date (y , m , d , 0 , 0 , 0 , 0 , time .UTC )
499
+
500
+ type test struct {
501
+ interval codersdk.InsightsReportInterval
502
+ withTemplate bool
503
+ }
504
+
505
+ tests := []test {
506
+ {codersdk .InsightsReportIntervalDay , true },
507
+ {codersdk .InsightsReportIntervalDay , false },
508
+ {"" , true },
509
+ {"" , false },
510
+ }
511
+
512
+ for _ , tt := range tests {
513
+ tt := tt
514
+ t .Run (fmt .Sprintf ("with interval=%q" , tt .interval ), func (t * testing.T ) {
515
+ t .Parallel ()
516
+
517
+ t .Run ("AsOwner" , func (t * testing.T ) {
518
+ t .Parallel ()
519
+
520
+ client := coderdtest .New (t , & coderdtest.Options {})
521
+ admin := coderdtest .CreateFirstUser (t , client )
522
+
523
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitShort )
524
+ defer cancel ()
525
+
526
+ var templateIDs []uuid.UUID
527
+ if tt .withTemplate {
528
+ version := coderdtest .CreateTemplateVersion (t , client , admin .OrganizationID , nil )
529
+ template := coderdtest .CreateTemplate (t , client , admin .OrganizationID , version .ID )
530
+ templateIDs = append (templateIDs , template .ID )
531
+ }
532
+
533
+ _ , err := client .UserLatencyInsights (ctx , codersdk.UserLatencyInsightsRequest {
534
+ StartTime : today ,
535
+ EndTime : time .Now ().UTC ().Truncate (time .Hour ).Add (time .Hour ), // Round up to include the current hour.
536
+ TemplateIDs : templateIDs ,
537
+ })
538
+ require .NoError (t , err )
539
+ })
540
+ t .Run ("AsTemplateAdmin" , func (t * testing.T ) {
541
+ t .Parallel ()
542
+
543
+ client := coderdtest .New (t , & coderdtest.Options {})
544
+ admin := coderdtest .CreateFirstUser (t , client )
545
+
546
+ templateAdmin , _ := coderdtest .CreateAnotherUser (t , client , admin .OrganizationID , rbac .RoleTemplateAdmin ())
547
+
548
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitShort )
549
+ defer cancel ()
550
+
551
+ var templateIDs []uuid.UUID
552
+ if tt .withTemplate {
553
+ version := coderdtest .CreateTemplateVersion (t , client , admin .OrganizationID , nil )
554
+ template := coderdtest .CreateTemplate (t , client , admin .OrganizationID , version .ID )
555
+ templateIDs = append (templateIDs , template .ID )
556
+ }
557
+
558
+ _ , err := templateAdmin .UserLatencyInsights (ctx , codersdk.UserLatencyInsightsRequest {
559
+ StartTime : today ,
560
+ EndTime : time .Now ().UTC ().Truncate (time .Hour ).Add (time .Hour ), // Round up to include the current hour.
561
+ TemplateIDs : templateIDs ,
562
+ })
563
+ require .NoError (t , err )
564
+ })
565
+ t .Run ("AsRegularUser" , func (t * testing.T ) {
566
+ t .Parallel ()
567
+
568
+ client := coderdtest .New (t , & coderdtest.Options {})
569
+ admin := coderdtest .CreateFirstUser (t , client )
570
+
571
+ regular , _ := coderdtest .CreateAnotherUser (t , client , admin .OrganizationID )
572
+
573
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitShort )
574
+ defer cancel ()
575
+
576
+ var templateIDs []uuid.UUID
577
+ if tt .withTemplate {
578
+ version := coderdtest .CreateTemplateVersion (t , client , admin .OrganizationID , nil )
579
+ template := coderdtest .CreateTemplate (t , client , admin .OrganizationID , version .ID )
580
+ templateIDs = append (templateIDs , template .ID )
581
+ }
582
+
583
+ _ , err := regular .UserLatencyInsights (ctx , codersdk.UserLatencyInsightsRequest {
584
+ StartTime : today ,
585
+ EndTime : time .Now ().UTC ().Truncate (time .Hour ).Add (time .Hour ), // Round up to include the current hour.
586
+ TemplateIDs : templateIDs ,
587
+ })
588
+ require .Error (t , err )
589
+ var apiErr * codersdk.Error
590
+ require .ErrorAs (t , err , & apiErr )
591
+ require .Equal (t , http .StatusNotFound , apiErr .StatusCode ())
592
+ })
593
+ })
594
+ }
595
+ }
0 commit comments