bmcweb service root interface

Changes:
    - Library to return response as it is back to the test.
    - Added code to disable warning and REST response code.
    - New test suite to verify service root interface.

Added Test Cases:
    - Login And Logout BMCweb
    - GET BMCweb Hypermedia Without Login
    - GET SessionService Resource With Login
    - GET SessionService Without Login
    - Login Using Invalid Token
    - Delete Session Using Valid login

Tested: Using latest OpenBMC master build.

Resolves  openbmc/openbmc-test-automation#1502

Change-Id: I24bbbd8d6c32371f28060275140bbeaf18d3414d
Signed-off-by: George Keishing <gkeishin@in.ibm.com>
diff --git a/lib/bmc_redfish.py b/lib/bmc_redfish.py
index 819dd63..73fbf93 100644
--- a/lib/bmc_redfish.py
+++ b/lib/bmc_redfish.py
@@ -73,11 +73,6 @@
         """
         self._rest_response_ = self._robj_.get('/redfish/v1/' + resource_path,
                                                *args, **kwargs)
-        if self._rest_response_.status != 200:
-            raise HTTPSBadRequestError("GET Session location: %s, "
-                                       "return code: %d"
-                                       % (self._session_location_,
-                                          self._rest_response_.status))
         return self._rest_response_
 
     def post(self, resource_path, *args, **kwargs):
@@ -92,11 +87,6 @@
         """
         self._rest_response_ = self._robj_.post('/redfish/v1/' + resource_path,
                                                 *args, **kwargs)
-        if self._rest_response_.status != 200:
-            raise HTTPSBadRequestError("POST Session location: %s, "
-                                       "return code: %d"
-                                       % (self._session_location_,
-                                          self._rest_response_.status))
         return self._rest_response_
 
     def patch(self, resource_path, *args, **kwargs):
@@ -110,11 +100,6 @@
         """
         self._rest_response_ = self._robj_.patch('/redfish/v1/' + resource_path,
                                                  *args, **kwargs)
-        if self._rest_response_.status != 200:
-            raise HTTPSBadRequestError("PATCH Session location: %s, "
-                                       "return code: %d"
-                                       % (self._session_location_,
-                                          self._rest_response_.status))
         return self._rest_response_
 
     def put(self, resource_path, actions, attr_data):
@@ -128,11 +113,6 @@
         """
         self._rest_response_ = self._robj_.put('/redfish/v1/' + resource_path,
                                                *args, **kwargs)
-        if self._rest_response_.status != 200:
-            raise HTTPSBadRequestError("PUT Session location: %s, "
-                                       "return code: %d"
-                                       % (self._session_location_,
-                                          self._rest_response_.status))
         return self._rest_response_
 
     def delete(self, resource_path):
@@ -144,11 +124,6 @@
                        (e.g. "/redfish/v1/SessionService/Sessions/8d1a9wiiNL").
         """
         self._rest_response_ = self._robj_.delete(resource_path)
-        if self._rest_response_.status != 200:
-            raise HTTPSBadRequestError("Session location: %s, "
-                                       "return code: %d"
-                                       % (self._session_location_,
-                                          self._rest_response_.status))
         return self._rest_response_
 
     def logout(self):
@@ -166,6 +141,8 @@
         """
 
         self._rest_response_ = self._robj_.get('/redfish/v1/' + resource_path)
+        if self._rest_response_.status != 200:
+            return self._rest_response_
 
         global resource_list
         resource_list = []
@@ -181,8 +158,7 @@
             self.walk_nested_dict(self._rest_response_.dict)
 
         resource_list.sort()
-        return json.dumps(resource_list, sort_keys=True,
-                          indent=4, separators=(',', ': '))
+        return resource_list
 
     def enumerate_request(self, resource_path):
         r"""
@@ -193,6 +169,8 @@
         """
 
         self._rest_response_ = self.list_request(resource_path)
+        if self._rest_response_.status != 200:
+            return self._rest_response_
 
         resource_dict = {}
         for resource in json.loads(self._rest_response_):
diff --git a/lib/bmc_redfish_resource.robot b/lib/bmc_redfish_resource.robot
index a0e2c3f..ba5d852 100644
--- a/lib/bmc_redfish_resource.robot
+++ b/lib/bmc_redfish_resource.robot
@@ -1,8 +1,10 @@
 *** Settings ***
 Documentation   BMC redfish resource keyword.
 
-Library         ../lib/bmc_redfish.py
+Library         bmc_redfish.py
 ...             ${OPENBMC_HOST}  ${OPENBMC_USERNAME}  ${OPENBMC_PASSWORD}
-...              WITH NAME    redfish
+...             WITH NAME  redfish
+Resource        rest_response_code.robot
+Library         disable_warning_urllib.py
 
 *** Keywords ***
diff --git a/redfish/service_root/test_service_root.robot b/redfish/service_root/test_service_root.robot
new file mode 100644
index 0000000..8dfd17a
--- /dev/null
+++ b/redfish/service_root/test_service_root.robot
@@ -0,0 +1,78 @@
+*** Settings ***
+Resource         ../../lib/resource.txt
+Resource         ../../lib/bmc_redfish_resource.robot
+
+Suite Teardown   redfish.Logout
+
+
+*** Test Cases ***
+
+Login And Logout BMCweb
+    [Documentation]  Login to BMCweb and then logout.
+    [Tags]  Login_And_Logout_BMCweb
+
+    redfish.Login
+    redfish.Logout
+
+
+GET BMCweb Hypermedia Without Login
+    [Documentation]  GET /redfish/v1 without login.
+    [Tags]  GET_BMCweb_Hypermedia_Without_Login
+
+    redfish.Logout
+    ${resp}=  redfish.Get  ${EMPTY}
+    Should Be Equal As Strings  ${resp.status}  ${HTTP_OK}
+
+
+GET SessionService Resource With Login
+    [Documentation]  Login to BMCweb and get /redfish/v1/SessionService.
+    [Tags]  GET_SessionService_Resource_With_Login
+
+    redfish.Login
+    ${resp}=  redfish.Get  SessionService
+    Should Be Equal As Strings  ${resp.status}  ${HTTP_OK}
+
+
+GET SessionService Without Login
+    [Documentation]  Get /redfish/v1/SessionService without login
+    [Tags]  GET_SessionService_Without_Login
+
+    redfish.Logout
+    ${resp}=  redfish.Get  SessionService
+    Should Be Equal As Strings  ${resp.status}  ${HTTP_UNAUTHORIZED}
+
+
+Login Using Invalid Token
+    [Documentation]  Login to BMCweb with invalid token.
+    [Tags]  Login_Using_Invalid_Token
+
+    redfish.Logout
+
+    Create Session  openbmc  ${AUTH_URI}
+
+    # Example: "X-Auth-Token: 3la1JUf1vY4yN2dNOwun"
+    ${headers}=  Create Dictionary  Content-Type=application/json
+    ...  X-Auth-Token=deadbeef
+
+    ${resp}=  Get Request
+    ...  openbmc  /redfish/v1/SessionService/Sessions  headers=${headers}
+
+    Should Be Equal As Strings  ${resp.status_code}  ${HTTP_UNAUTHORIZED}
+
+
+Delete Session Using Valid login
+    [Documentation]  Delete a session using valid login.
+    [Tags]  Delete_Session_Using_Valid_Login
+
+    redfish.Login
+
+    # Example o/p:
+    # [{'@odata.id': '/redfish/v1/SessionService/Sessions/bOol3WlCI8'},
+    #  {'@odata.id': '/redfish/v1/SessionService/Sessions/Yu3xFqjZr1'}]
+    ${resp_list}=  redfish.Get  SessionService/Sessions
+
+    redfish.Delete  ${resp_list.dict["Members"][0]["@odata.id"]}
+
+    ${resp}=  redfish.Get  SessionService/Sessions
+    Should Not Contain  ${resp.dict["Members"]}  ${resp_list.dict["Members"][0]["@odata.id"]}
+
diff --git a/redfish_test/test_poweron.robot b/redfish/test_poweron.robot
similarity index 100%
rename from redfish_test/test_poweron.robot
rename to redfish/test_poweron.robot
diff --git a/redfish_test/test_redfish_basic_poweron.robot b/redfish/test_redfish_basic_poweron.robot
similarity index 100%
rename from redfish_test/test_redfish_basic_poweron.robot
rename to redfish/test_redfish_basic_poweron.robot
diff --git a/redfish_test/test_redfish_interfaces.robot b/redfish/test_redfish_interfaces.robot
similarity index 100%
rename from redfish_test/test_redfish_interfaces.robot
rename to redfish/test_redfish_interfaces.robot