@@ -574,4 +574,164 @@ var _ = Describe("Client", func() {
574574 })
575575 })
576576 })
577+
578+ Describe ("GetSecurityGroups" , func () {
579+ BeforeEach (func () {
580+ fakeJSONClient .DoStub = func (method , route string , reqData , respData interface {}, token string ) error {
581+ err := json .Unmarshal ([]byte (fixtures .OneSecurityGroup ), respData )
582+ Expect (err ).ToNot (HaveOccurred ())
583+ return nil
584+ }
585+ })
586+
587+ It ("polls the Cloud Controller successfully" , func () {
588+ sgs , err := client .GetSecurityGroups ("some-token" )
589+ Expect (err ).NotTo (HaveOccurred ())
590+ Expect (fakeJSONClient .DoCallCount ()).To (Equal (1 ))
591+
592+ method , route , reqData , _ , _ := fakeJSONClient .DoArgsForCall (0 )
593+
594+ Expect (method ).To (Equal ("GET" ))
595+ Expect (route ).To (Equal ("/v3/security_groups" ))
596+ Expect (reqData ).To (BeNil ())
597+
598+ Expect (len (sgs )).To (Equal (1 ))
599+ Expect (sgs [0 ].Name ).To (Equal ("my-group0" ))
600+ Expect (sgs [0 ].GUID ).To (Equal ("b85a788e-671f-4549-814d-e34cdb2f539a" ))
601+ Expect (sgs [0 ].GloballyEnabled .Running ).To (BeTrue ())
602+ Expect (sgs [0 ].GloballyEnabled .Staging ).To (BeFalse ())
603+ Expect (sgs [0 ].Rules [0 ].Protocol ).To (Equal ("tcp" ))
604+ Expect (sgs [0 ].Rules [1 ].Protocol ).To (Equal ("icmp" ))
605+ })
606+
607+ Context ("when there are no security groups" , func () {
608+ BeforeEach (func () {
609+ fakeJSONClient .DoStub = func (method , route string , reqData , respData interface {}, token string ) error {
610+ err := json .Unmarshal ([]byte (fixtures .NoSecurityGroups ), respData )
611+ Expect (err ).ToNot (HaveOccurred ())
612+ return nil
613+ }
614+ })
615+
616+ It ("Returns them all" , func () {
617+ sgs , err := client .GetSecurityGroups ("some-token" )
618+ Expect (err ).NotTo (HaveOccurred ())
619+ Expect (fakeJSONClient .DoCallCount ()).To (Equal (1 ))
620+
621+ method , route , reqData , _ , _ := fakeJSONClient .DoArgsForCall (0 )
622+
623+ Expect (method ).To (Equal ("GET" ))
624+ Expect (route ).To (Equal ("/v3/security_groups" ))
625+ Expect (reqData ).To (BeNil ())
626+
627+ Expect (len (sgs )).To (Equal (0 ))
628+ })
629+ })
630+
631+ Context ("when multiple security groups are returned" , func () {
632+ BeforeEach (func () {
633+ fakeJSONClient .DoStub = func (method , route string , reqData , respData interface {}, token string ) error {
634+ err := json .Unmarshal ([]byte (fixtures .TwoSecurityGroups ), respData )
635+ Expect (err ).ToNot (HaveOccurred ())
636+ return nil
637+ }
638+ })
639+
640+ It ("Returns them all" , func () {
641+ sgs , err := client .GetSecurityGroups ("some-token" )
642+ Expect (err ).NotTo (HaveOccurred ())
643+ Expect (fakeJSONClient .DoCallCount ()).To (Equal (1 ))
644+
645+ method , route , reqData , _ , _ := fakeJSONClient .DoArgsForCall (0 )
646+
647+ Expect (method ).To (Equal ("GET" ))
648+ Expect (route ).To (Equal ("/v3/security_groups" ))
649+ Expect (reqData ).To (BeNil ())
650+
651+ Expect (len (sgs )).To (Equal (2 ))
652+ Expect (sgs [1 ].Name ).To (Equal ("my-group2" ))
653+ Expect (sgs [1 ].GUID ).To (Equal ("second-guid" ))
654+ Expect (sgs [1 ].GloballyEnabled .Running ).To (BeFalse ())
655+ Expect (sgs [1 ].GloballyEnabled .Staging ).To (BeTrue ())
656+ Expect (sgs [1 ].Rules [0 ].Protocol ).To (Equal ("tcp" ))
657+ Expect (sgs [1 ].Rules [0 ].Ports ).To (Equal ("53" ))
658+ })
659+ })
660+
661+ Context ("when there are multiple pages" , func () {
662+ BeforeEach (func () {
663+ fakeJSONClient .DoStub = func (method , route string , reqData , respData interface {}, token string ) error {
664+ if route == "/v3/security_groups?page=2&per_page=1" {
665+ err := json .Unmarshal ([]byte (fixtures .SecurityGroupsMultiplePagesPg2 ), respData )
666+ Expect (err ).ToNot (HaveOccurred ())
667+ } else if route == "/v3/security_groups?page=3&per_page=1" {
668+ err := json .Unmarshal ([]byte (fixtures .SecurityGroupsMultiplePagesPg3 ), respData )
669+ Expect (err ).ToNot (HaveOccurred ())
670+ } else {
671+ err := json .Unmarshal ([]byte (fixtures .SecurityGroupsMultiplePages ), respData )
672+ Expect (err ).ToNot (HaveOccurred ())
673+ }
674+ return nil
675+ }
676+ })
677+
678+ It ("returns all the security groups" , func () {
679+ sgs , err := client .GetSecurityGroups ("some-token" )
680+ Expect (err ).NotTo (HaveOccurred ())
681+ Expect (fakeJSONClient .DoCallCount ()).To (Equal (3 ))
682+
683+ method , route , reqData , _ , token := fakeJSONClient .DoArgsForCall (0 )
684+
685+ Expect (method ).To (Equal ("GET" ))
686+ Expect (route ).To (Equal ("/v3/security_groups" ))
687+ Expect (reqData ).To (BeNil ())
688+ Expect (token ).To (Equal ("bearer some-token" ))
689+
690+ method , route , reqData , _ , token = fakeJSONClient .DoArgsForCall (1 )
691+
692+ Expect (method ).To (Equal ("GET" ))
693+ Expect (route ).To (Equal ("/v3/security_groups?page=2&per_page=1" ))
694+ Expect (reqData ).To (BeNil ())
695+ Expect (token ).To (Equal ("bearer some-token" ))
696+
697+ method , route , reqData , _ , token = fakeJSONClient .DoArgsForCall (2 )
698+
699+ Expect (method ).To (Equal ("GET" ))
700+ Expect (route ).To (Equal ("/v3/security_groups?page=3&per_page=1" ))
701+ Expect (reqData ).To (BeNil ())
702+ Expect (token ).To (Equal ("bearer some-token" ))
703+
704+ Expect (len (sgs )).To (Equal (3 ))
705+ Expect (sgs [0 ].Name ).To (Equal ("my-group0" ))
706+ Expect (sgs [0 ].GUID ).To (Equal ("b85a788e-671f-4549-814d-e34cdb2f539a" ))
707+ Expect (sgs [0 ].GloballyEnabled .Running ).To (BeTrue ())
708+ Expect (sgs [0 ].GloballyEnabled .Staging ).To (BeFalse ())
709+ Expect (sgs [0 ].Rules [0 ].Protocol ).To (Equal ("tcp" ))
710+ Expect (sgs [0 ].Rules [1 ].Protocol ).To (Equal ("icmp" ))
711+ Expect (sgs [1 ].Name ).To (Equal ("my-group2" ))
712+ Expect (sgs [1 ].GUID ).To (Equal ("second-guid" ))
713+ Expect (sgs [1 ].GloballyEnabled .Running ).To (BeFalse ())
714+ Expect (sgs [1 ].GloballyEnabled .Staging ).To (BeTrue ())
715+ Expect (sgs [1 ].Rules [0 ].Protocol ).To (Equal ("tcp" ))
716+ Expect (sgs [1 ].Rules [0 ].Ports ).To (Equal ("53" ))
717+ Expect (sgs [2 ].Name ).To (Equal ("my-group3" ))
718+ Expect (sgs [2 ].GUID ).To (Equal ("third-guid" ))
719+ Expect (sgs [2 ].GloballyEnabled .Running ).To (BeTrue ())
720+ Expect (sgs [2 ].GloballyEnabled .Staging ).To (BeTrue ())
721+ Expect (sgs [2 ].Rules [0 ].Protocol ).To (Equal ("tcp" ))
722+ Expect (sgs [2 ].Rules [0 ].Ports ).To (Equal ("123" ))
723+ })
724+ })
725+
726+ Context ("when the json client returns an error" , func () {
727+ BeforeEach (func () {
728+ fakeJSONClient .DoReturns (errors .New ("kissa ja undulaatti" ))
729+ })
730+
731+ It ("returns a helpful error" , func () {
732+ _ , err := client .GetSubjectSpaces ("some-token" , "some-subject-id" )
733+ Expect (err ).To (MatchError (ContainSubstring ("json client do: kissa ja undulaatti" )))
734+ })
735+ })
736+ })
577737})
0 commit comments